关闭主窗口时 WPF 应用程序不会关闭

问题描述:

我习惯于在 Visual Studio 中进行 WinForms 编程,但我想尝试一下 WPF.

I'm used to WinForms programming in Visual Studio, but I wanted to give WPF a try.

我在我的项目中添加了另一个窗口,称为 Window01.主窗口称为 MainWindow.在 public MainWindow() 构造函数之前,我声明 Window01:

I added another window to my project, called Window01. The main window is called MainWindow. Before the public MainWindow() constructor I declare Window01:

Window01 w1;

现在我实例化这个窗口:

Now I instantiate this window in:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    w1 = new Window01();            
}

我有一个显示窗口的按钮:w1.ShowDialog();.

I have a button where the window is shown: w1.ShowDialog();.

这里有趣"的一点是,如果我启动应用程序(带调试)并在几秒钟后退出(我没有在应用程序中做任何事情),Visual Studio 不会停止调试,好像应用程序仍在运行.

The 'funny' thing here is the fact that if I start the application (with debugging) and exit it a few seconds after (I don't do anything in the application), Visual Studio doesn't stop debugging as if the application is still running.

如果我将行 w1 = new Window01(); 移动到按钮单击方法,这意味着在 ShowDialog() 上方,Visual Studio 运行正常 - 即, 退出应用程序时调试停止.

If I move the line w1 = new Window01(); to the button click method, meaning just above ShowDialog(), Visual Studio is behaving properly - that is, the debugging stops when I exit the application.

为什么会有这种奇怪的行为?

Why this strange behaviour?

在您的 MainWindow.xaml.cs 中,尝试这样做:

In your MainWindow.xaml.cs, try doing this:

protected override void OnClosed(EventArgs e)
{
    base.OnClosed(e);

    Application.Current.Shutdown();
}

通过此链接,您还可以在 XAML 中设置 ShutdownMode:

Per this link, you can also set the ShutdownMode in XAML:

http://msdn.microsoft.com/en-us/library/system.windows.application.shutdownmode.aspx

<Application
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="MainWindow.xaml"
    ShutdownMode="OnExplicitShutdown"
    >
</Application>

仅当ApplicationShutdown 方法被调用时,应用程序才会停止运行.关闭可以隐式或显式发生,由 ShutdownMode 属性的值指定.

Applications stop running only when the Shutdown method of the Application is called. Shut down can occur implicitly or explicitly, as specified by the value of the ShutdownMode property.

如果将 ShutdownMode 设置为 OnLastWindowClose,Windows Presentation Foundation (WPF) 会在应用程序中的最后一个窗口关闭时隐式调用 Shutdown,即使设置了任何当前实例化的窗口作为主窗口(参见 MainWindow).

If you set ShutdownMode to OnLastWindowClose, Windows Presentation Foundation (WPF) implicitly calls Shutdown when the last window in an application closes, even if any currently instantiated windows are set as the main window (see MainWindow).

OnMainWindowCloseShutdownMode 会导致 WPF 在 MainWindow 关闭时隐式调用 Shutdown,即使其他窗口当前处于打开状态.

A ShutdownMode of OnMainWindowClose causes WPF to implicitly call Shutdown when the MainWindow closes, even if other windows are currently open.

某些应用程序的生命周期可能不依赖于主窗口或最后一个窗口的关闭时间,或者可能根本不依赖于窗口.对于这些场景,您需要将 ShutdownMode 属性设置为 OnExplicitShutdown,这需要显式调用 Shutdown 方法来停止应用程序.否则,应用程序将继续在后台运行.

The lifetime of some applications may not be dependent on when the main window or last window is closed, or may not be dependent on windows at all. For these scenarios you need to set the ShutdownMode property to OnExplicitShutdown, which requires an explicit Shutdown method call to stop the application. Otherwise, the application continues running in the background.

ShutdownMode 可以通过 XAML 以声明方式配置,也可以通过代码以编程方式配置.

ShutdownMode can be configured declaratively from XAML or programmatically from code.

此属性仅可从创建 Application 对象的线程使用.

This property is available only from the thread that created the Application object.

在您的情况下,应用程序没有关闭,因为您可能使用了默认的 OnLastWindowClose:

In your case, the app isn't closing because you're probably using the default OnLastWindowClose:

如果将 ShutdownMode 设置为 OnLastWindowClose,WPF 在应用程序中的最后一个窗口关闭时隐式调用 Shutdown,即使任何当前实例化的窗口被设置为主窗口(见MainWindow).

If you set ShutdownMode to OnLastWindowClose, WPF implicitly calls Shutdown when the last window in an application closes, even if any currently instantiated windows are set as the main window (see MainWindow).

由于您正在打开一个新窗口而不是关闭它,因此不会调用 shutdown.

Since you're opening a new window, and not closing it, shutdown doesn't get called.