如何以编程方式退出 WPF 应用程序? [英] How do I exit a WPF application programmatically?
问题描述
应该如何退出应用程序,例如当用户单击文件"菜单中的退出"菜单项时?
我试过了:
this.Dispose();this.Exit();Application.ShutDown();Application.Exit();Application.Dispose();
在许多其他人中.没有任何效果.
要退出您的应用程序,您可以调用
System.Windows.Application.Current.Shutdown();
如Application.Shutdown
方法,您还可以通过指定 关机模式:
Windows Presentation Foundation (WPF) 在以下情况下隐式调用关闭:
- 当 ShutdownMode 设置为 OnLastWindowClose 时.
- 当 ShutdownMode 设置为 OnMainWindowClose 时.
- 当用户结束会话并且 SessionEnding 事件未处理或未取消处理时.
另请注意,Application.Current.Shutdown();
只能从创建 Application
对象的线程调用,即通常是主线程.>
How is one supposed to exit an application such as when the user clicks on the Exit menu item from the File menu?
I have tried:
this.Dispose();
this.Exit();
Application.ShutDown();
Application.Exit();
Application.Dispose();
Among many others. Nothing works.
To exit your application you can call
System.Windows.Application.Current.Shutdown();
As described in the documentation to the Application.Shutdown
method you can also modify the shutdown behavior of your application by specifying a ShutdownMode:
Shutdown is implicitly called by Windows Presentation Foundation (WPF) in the following situations:
- When ShutdownMode is set to OnLastWindowClose.
- When the ShutdownMode is set to OnMainWindowClose.
- When a user ends a session and the SessionEnding event is either unhandled, or handled without cancellation.
Please also note that Application.Current.Shutdown();
may only be called from the thread that created the Application
object, i.e. normally the main thread.
这篇关于如何以编程方式退出 WPF 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!