wpf登录界面实现跳转
时间: 2023-11-01 19:06:48 浏览: 257
在WPF中实现登录界面跳转,可以使用页面导航的方式。具体实现步骤如下:
1.在WPF项目中创建两个页面,一个是登录页面(Login.xaml),一个是主页面(MainPage.xaml)。
2.在Login.xaml页面中添加登录按钮,并绑定登录事件。
3.在登录事件中,判断用户名和密码是否正确,如果正确,则使用NavigationService.Navigate方法跳转到主页面(MainPage.xaml)。
4.在MainPage.xaml页面中添加返回按钮,并绑定返回事件。在返回事件中,使用NavigationService.GoBack方法返回到登录页面(Login.xaml)。
示例代码如下:
Login.xaml页面:
```
<Grid>
<StackPanel>
<Label Content="用户名" />
<TextBox x:Name="txtUserName" />
<Label Content="密码" />
<PasswordBox x:Name="txtPassword" />
<Button Content="登录" Click="btnLogin_Click" />
</StackPanel>
</Grid>
```
Login.xaml.cs页面:
```
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
// 判断用户名和密码是否正确
if (txtUserName.Text == "admin" && txtPassword.Password == "123456")
{
// 跳转到主页面
NavigationService.Navigate(new Uri("MainPage.xaml", UriKind.Relative));
}
else
{
MessageBox.Show("用户名或密码错误!");
}
}
```
MainPage.xaml页面:
```
<Grid>
<StackPanel>
<Label Content="这是主页面" />
<Button Content="返回" Click="btnBack_Click" />
</StackPanel>
</Grid>
```
MainPage.xaml.cs页面:
```
private void btnBack_Click(object sender, RoutedEventArgs e)
{
// 返回到登录页面
NavigationService.GoBack();
}
```
阅读全文
相关推荐
















