laconicdev
laconicdev

Reputation: 6440

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?

Upvotes: 0

Views: 216

Answers (1)

Rachel
Rachel

Reputation: 132548

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

Upvotes: 2

Related Questions