如何以编程方式退出 WPF 应用程序? [英] How do I exit a WPF application programmatically?

查看:41
本文介绍了如何以编程方式退出 WPF 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应该如何退出应用程序,例如当用户单击文件"菜单中的退出"菜单项时?

我试过了:

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

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