Application.ShutdownMode 属性

定义

获取或设置导致调用 Shutdown() 方法的条件。Gets or sets the condition that causes the Shutdown() method to be called.

public:
 property System::Windows::ShutdownMode ShutdownMode { System::Windows::ShutdownMode get(); void set(System::Windows::ShutdownMode value); };
public System.Windows.ShutdownMode ShutdownMode { get; set; }
member this.ShutdownMode : System.Windows.ShutdownMode with get, set
Public Property ShutdownMode As ShutdownMode

属性值

一个 ShutdownMode 枚举值。A ShutdownMode enumeration value. 默认值为 OnLastWindowCloseThe default value is OnLastWindowClose.

示例

下面的示例演示如何配置 ShutdownMode 以指定必须显式关闭应用程序。The following example shows how to configure ShutdownMode to specify that an application must be closed explicitly.

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

注解

只有在调用应用程序的 Shutdown 方法时,应用程序才会停止运行。Applications stop running only when the Shutdown method of the Application is called. 关机可以隐式或显式方式发生,如 ShutdownMode 属性的值所指定。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).

MainWindow 关闭时,ShutdownMode OnMainWindowClose 会导致 WPF 隐式调用 Shutdown,即使当前打开了其他窗口也是如此。A ShutdownMode of OnMainWindowClose causes WPF to implicitly call Shutdown when the MainWindow closes, even if other windows are currently open.

某些应用程序的生存期可能不依赖于主窗口或最后一个窗口关闭的时间,也可能根本不依赖于 windows。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. 对于这些情况,需要将 ShutdownMode 属性设置为 OnExplicitShutdown,这需要使用显式 Shutdown 方法调用来停止应用程序。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.

可以通过代码以声明方式从 XAMLXAML 或以编程方式配置 ShutdownModeShutdownMode can be configured declaratively from XAMLXAML or programmatically from code.

此属性仅在创建 Application 对象的线程中可用。This property is available only from the thread that created the Application object.

适用于

另请参阅