简体   繁体   English

关闭从App.xaml启动的模态窗口会触发WindowClosing事件

[英]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. 我有一个WPF应用程序,作为启动的一部分,我需要显示一个模态窗口,以从用户获取一些信息。 I create and show the window from inside App.xaml.cs_ApplicationStartup. 我从App.xaml.cs_ApplicationStartup中创建并显示窗口。 In the window's ok button, I call the Close() method. 在窗口的ok按钮中,我调用Close()方法。 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. 但是,主应用程序窗口立即收到WindowClosing事件并终止。 App.xaml code: App.xaml代码:

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 更改Application.ShutdownMode属性

The default is OnLastWindowClose , which means the application will shut down when the last window closes. 默认值为OnLastWindowClose ,这意味着应用程序将在最后一个窗口关闭时关闭。 The other two options are OnMainWindowClose and OnExplicitShutdown 另外两个选项是OnMainWindowCloseOnExplicitShutdown

I typically use OnExplicitShutdown , which means Shutdown() needs to be called for the application to exit 我通常使用OnExplicitShutdown ,这意味着需要调用Shutdown()才能退出应用程序

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

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