How to implement page redirection in WPF login screen?
In WPF, you can achieve navigation by using the following methods:
- Utilize the Frame control: add a Frame control on the login interface, and in the click event of the login button, navigate to the target interface using the Frame control’s Navigate method. For example:
private void LoginButton_Click(object sender, RoutedEventArgs e)
{
frame.Navigate(new TargetPage());
}
- Using the NavigationWindow control: You can create a new NavigationWindow window to host the login and target interface, and open a new NavigationWindow window in the click event of the login button. For example:
private void LoginButton_Click(object sender, RoutedEventArgs e)
{
NavigationWindow window = new NavigationWindow();
window.Content = new TargetPage();
window.Show();
this.Close();
}
- Utilize the Content property of the MainWindow window: in the XAML file of the MainWindow window, define two Grids for the login and target interface, and switch the Visibility property of the two Grids in the click event of the login button. For example:
The main window of the application is in the file named MainWindow.xaml.
<Grid x:Name="LoginGrid">
<!-- 登录界面的内容 -->
</Grid>
<Grid x:Name="TargetGrid" Visibility="Hidden">
<!-- 目标界面的内容 -->
</Grid>
The code-behind file for the MainWindow.xaml.
private void LoginButton_Click(object sender, RoutedEventArgs e)
{
LoginGrid.Visibility = Visibility.Hidden;
TargetGrid.Visibility = Visibility.Visible;
}
The above are three common ways to implement navigation, you can choose the method that best fits your needs.