应该如何退出应用程序,例如当用户单击"file"菜单中的“退出”菜单项时?
我试过:
this.Dispose();
this.Exit();
Application.ShutDown();
Application.Exit();
Application.Dispose();
等等。什么都没用。
最佳答案
要退出您的应用程序,您可以调用
System.Windows.Application.Current.Shutdown();
如 Application.Shutdown
的文档中所述方法,您还可以通过指定 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.
另请注意 Application.Current.Shutdown();
只能从创建 Application
的线程调用对象,即通常是主线程。
关于c# - 如何以编程方式退出 WPF 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2820357/