简体   繁体   中英

Closing a modal window launched from App.xaml triggers WindowClosing event

I have a WPF application, where as part of the startup, I need to show a modal window to get some informatin from the user. I create and show the window from inside App.xaml.cs_ApplicationStartup. In the window's ok button, I call the Close() method. Once the information is retrieved, I want to show the main application window. However, the main application window immediately received a WindowClosing event and terminates. App.xaml code:

private void ApplicationStartup(object sender, StartupEventArgs e)
{
    if (Settings.Default.AskForLoginCredentials)
    {
        var loginWindow = new LoginCredentialsWindow();
        bool? retVal = loginWindow.ShowDialog();
        if (retVal.HasValue && retVal.Value)
        {
            string un = loginWindow.UserName;
            string pw = loginWindow.Password;
        }
        else
        {
            _logger.Info("Credential request prompt was refused. Exiting Application.");
            return;
        }
    }

    MainAppWindow window = new MainAppWindow();
    window.Show();
}

What is happening here?

Change the Application.ShutdownMode property

The default is OnLastWindowClose , which means the application will shut down when the last window closes. The other two options are OnMainWindowClose and OnExplicitShutdown

I typically use OnExplicitShutdown , which means Shutdown() needs to be called for the application to exit

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