简体   繁体   English

WPF:window.ShowDialog()然后window.Show()-应用已停止

[英]WPF: window.ShowDialog() then window.Show() - application stopped

I try to show the authentication window, then open the main window, but when you close the authorization window, application is stopped 我尝试显示身份验证窗口,然后打开主窗口,但是当您关闭授权窗口时,应用程序停止了

private void App_OnStartup(object sender, StartupEventArgs e)
{
  new LoginWindow().ShowDialog();
  new MainWindow().Show();
  // Then application stopped
}

BUT! 但!

If the display window authentication by using method Show(), the application does not close after closing the authorization window 如果使用方法Show()对显示窗口进行身份验证,则关闭授权窗口后应​​用程序不会关闭

private void App_OnStartup(object sender, StartupEventArgs e)
{
  new LoginWindow().Show();
  new MainWindow().Show();
  // Then application running
}

Why is this behavior??? 为什么会这样呢???

Thanks to Eran Otzap! 感谢Eran Otzap!

Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown; Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;

Is working! 正在工作!

By default, when the main windows of the application is closed, then the application is closed. 默认情况下,当应用程序的主窗口关闭时,应用程序将关闭。

According to the documentation, "Application.MainWindow is automatically set with a reference to the first Window object to be instantiated in the AppDomain." 根据文档,“ Application.MainWindow会自动设置为对要在AppDomain中实例化的第一个Window对象的引用。”

To get around this, you can try to create first a MainWindow object (without calling Show()), then create and show the login dialog, and then show the main window. 要解决此问题,您可以尝试先创建一个MainWindow对象(不调用Show()),然后创建并显示登录对话框,然后显示主窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM