简体   繁体   中英

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

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!

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."

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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