Home > OS >  Navigate between windows in WinUI 3
Navigate between windows in WinUI 3

Time:07-20

I am building a program where the main window (MainWindow) has a "Login" button. Login button takes you to LoginWindow. How do I navigate from the MainWindow to the LoginWindow on the click of a button? Or maybe activate the login window and deactivate main window? I am very new to WinUI and Windows App SDK. Thanks! Main window and the login button function

CodePudding user response:

You can use the ContentDialog and put your LoginPage in it.

MainWindow.xaml

<StackPanel x:Name="Root">
    <Button
        Click="LoginButton_Click"
        Content="Login" />
</StackPanel>

MainWindow.xaml.cs

private async void LoginButton_Click(object sender, RoutedEventArgs e)
{
    ContentDialog dialog = new();
    dialog.XamlRoot = Root.XamlRoot;
    dialog.Title = "Login";
    dialog.CloseButtonText = "Cancel";
    dialog.Content = new LoginPage();
    ContentDialogResult result = await dialog.ShowAsync();
}

LoginPage.xaml

<StackPanel>
    <TextBlock Text="Username" />
    <TextBox />
    <TextBlock Text="Password" />
    <PasswordBox />
</StackPanel>
  • Related