之后,新的窗口程序不会停止 [英] Program doesn't stop after new Window

查看:110
本文介绍了之后,新的窗口程序不会停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

或许对C#一个愚蠢的问题,WPF,.NET 4.0:

Maybe a stupid question for C#, WPF, .NET 4.0:

如果我做一个新的派生窗口类,不调用的ShowDialog在此窗口,我的程序没有关闭了上接近。

If I do a new on a window derived class and don't call ShowDialog on this window, my program doesn't shut down anymore on close.

例如:

        Window d = new Window();
        //d.ShowDialog();

为什么会这样呢?

Why is this so?

我不想显示窗口,我只是想用这个对象的某种目的。所以,我有什么做的,为了让我的程序关闭之后?

I don't want to show the window, I just want to use this object for some purpose. So what do I have to do in order to allow my program to shut down afterwards?

推荐答案

这很可能你已经配置你的应用程序关闭的 的时,所有的窗户都被关闭,只有创建是永远不会被关闭的新窗口,您的应用程序永远不会被关闭。

It's very likely that you've configured your application to close only when all of its windows have been closed, and by creating a new window that never gets closed, your application never gets shut down.

此设置由<一个管辖href="http://msdn.microsoft.com/en-us/library/system.windows.application.shutdownmode.aspx"><$c$c>Application.ShutdownMode属性,该指定导致<$c$c>Shutdown法被调用。从本质上讲,你有三个选择。

This setting is governed by the Application.ShutdownMode property, which specifies the condition that causes the Shutdown method to be called. Essentially, you have three options.

  1. 第一,你所遇到的,不会关闭应用程序,除非它已经创建的所有窗口都关闭。这是无关紧要是否已经关闭了用户或编程调用他们的关闭方法。

这个选项是由 Application.ShutdownMode 属性设置为 OnLastWindowClose 指定。

This option is specified by setting the Application.ShutdownMode property to OnLastWindowClose.

第二种方法需要的事实,几乎所有的应用程序都有一个主窗口(这是你显示,当您的应用程序推出的第一个)的优势,并具有运行时会自动关闭你的整个应用程序(和所有子窗口)时,该主窗口已关闭。再次,这是不相关的用户窗口是否关闭,或者你通过code将其关闭。

The second method takes advantage of the fact that almost all applications have a "main" window (which is the first one you display when your application is launched), and has the runtime automatically close your entire application (and all child windows) when this main window is closed. Again, it is irrelevant whether the window is closed by the user or you close it through code.

这个选项是由 Application.ShutdownMode 属性设置为 OnMainWindowClose 指定。

This option is specified by setting the Application.ShutdownMode property to OnMainWindowClose.

第三个选项,基本上表明你要手动管理的事情。直到调用它不会关闭应用程序的<一个href="http://msdn.microsoft.com/en-us/library/system.windows.application.shutdown.aspx"><$c$c>Shutdown 的方法,通过自己的code。

The third option essentially indicates that you're going to manage things manually. It will not close the application until you call the Shutdown method yourself through code.

这个选项是由 Application.ShutdownMode 属性设置为 OnExplicitShutdown 指定。

This option is specified by setting the Application.ShutdownMode property to OnExplicitShutdown.

我的建议,在这种情况下,就是你设置的第二的选项,让您的应用程序自动关闭,当用户关闭主窗口。这将prevent可能仍然由$ P $从收盘pventing你的应用程序中显示的任何杂散子窗口。这消除了需要写在Stecya's回答,并允许运行时自动处理所有这一切都为你。

My recommendation, in this case, is that you set the second option, and have your app automatically close whenever the user closes the main window. That will prevent any stray child windows that may still be shown from preventing your app from closing. This eliminates the need to write the code shown in Stecya's answer, and lets the runtime handle all of this for you automatically.

这篇关于之后,新的窗口程序不会停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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