WPF应用程序不会关闭时关闭主窗口 [英] WPF App Doesn't Shut Down When Closing Main Window

查看:167
本文介绍了WPF应用程序不会关闭时关闭主窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经习惯了的WinForms在Visual Studio编程,但我想给WPF一试。

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

我添加了另一个窗口,我的项目,叫做Window01。主窗口被称为主窗口。在之前公开主窗口()构造我宣布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();

在'有趣'在这里的事情是事实,如果我启动该应用程序(调试),并退出它在几秒钟后(我不这样做的任何应用程序),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 =新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();
}

每此链接,还可以设置ShutdownMode在XAML:

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

<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.application.shutdownmode.aspx\">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>

应用程序停止运行,只有当应用程序的关闭方法被调用。关闭可以或明或暗地出现,由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基金会(WPF)隐式调用关机时,在应用程序中的最后一个窗口关闭,即使任何当前实例化的窗口都设置主窗口(见主窗口)。

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

OnMainWindowClose的ShutdownMode会导致Windows presentation基金会(WPF)隐式调用shutdown当主窗口关闭,即使其他窗口是当前打开的。

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

的一些应用中的寿命可以不依赖当主窗口或最后窗口被关闭,或者可以不依赖于窗口的。对于这些情况下,您需要设置ShutdownMode属性OnExplicitShutdown,这需要一个明确的关机方法调用停止应用程序。否则,应用程序将继续在后台运行。

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从code配置或编程。

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时,Windows presentation基金会(WPF)隐式调用关机时,在应用程序中的最后一个窗口关闭,即使任何当前实例化的窗口都设置主窗口(见主窗口)。

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

既然你打开一个新窗口,而不是将其关闭,停产不会被调用。

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

这篇关于WPF应用程序不会关闭时关闭主窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆