登录后如何关闭DialogWIndow并显示主窗口? [英] How to Close a DialogWIndow after login and display main window?
问题描述
我有一个登录窗口和一个主窗口.现在,我想在成功登录后关闭登录窗口,然后我的主窗口应该打开.但到目前为止,我的两个窗户都打开了,这让我很恼火.
I have a login window and a main window. Now, I want to close the login window after a successful login and my main window should then open. But so far, I am getting both windows opening which is irritating me.
我在 app.xaml 后面的代码中尝试了下面的代码,但它关闭了整个应用程序...
I have tried the code below in the code behind of app.xaml but it closes the whole application...
DataRepository repository = new DataRepository();
ViewModelPassword viewModelPassword = new ViewModelPassword(repository);
passwordDialog passwordDialog = new PasswordDialog();
passwordDialog.DataContext = viewModelPassword;
viewModelPassword.RequestClose += (s, ee) => passwordDialog.Close();
passwordDialog.ShowDialog();
Mainviewmodel viewModel = new Mainviewmodel (repository, viewModelPassword.Login);
MainWindow window = new MainWindow();
window.DataContext = viewModel;
window.Show();
base.OnStartup(e);
login 命令发起成功登录后,我想显示主窗口.
After the login command has initiated a successful login, I want to display the main window.
推荐答案
您在运行启动代码之前就显示了两个 Windows,因此在应用程序启动时,两个窗口都显示了.
You are showing both Windows before even running the startup code, so on application startup both windows are showing.
这是我通常用于此场景的代码:
Here's the code I typically use for this scenario:
protected override void OnStartup(StartupEventArgs e)
{
// Run startup code first
base.OnStartup(e);
// Create Login Window and Show it
var login = new LoginDialog();
var loginVm = new LoginViewModel();
login.DataContext = loginVm;
login.ShowDialog();
// If login window didn't return true (login failed), exit application
if (!login.DialogResult.GetValueOrDefault())
{
Environment.Exit(0);
}
// Providing we have a successful login, start main application window
var app = new ShellView();
var context = new ShellViewModel(loginVm.CurrentUser);
app.DataContext = context;
app.Show();
}
这篇关于登录后如何关闭DialogWIndow并显示主窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!