如何退出一个WPF应用程序编程? [英] How to exit a WPF app programmatically?

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

问题描述

在几年里,我一直在使用C#(的WinForms),我从来没有使用WPF。但是,现在我很喜欢WPF,但我不知道怎么我应该退出我的应用程序,当用户点击文件菜单退出菜单项。

In the few years I've been using C# (WinForms), I've never used WPF. But, now I love WPF, but I don't know how I am supposed to exit my application when the user clicks on the Exit menu item from the File menu.

我曾尝试:

this.Dispose();
this.Exit();
Application.ShutDown();
Application.Exit();
Application.Dispose();

在许多人。没有工作的。

Among many others. Nothing works.

推荐答案

要退出你的应用程序可以调用

To exit your application you can call

Application.Current.Shutdown();

由于描述的文档中的 Application.Shutdown 方法,你也可以通过指定<一个修改应用程序的关闭行为href="http://msdn.microsoft.com/en-us/library/system.windows.application.shutdownmode.aspx">ShutdownMode:

关机被隐含在下列情况下,所谓的由Windows presentation基金会(WPF):

Shutdown is implicitly called by Windows Presentation Foundation (WPF) in the following situations:

      
  • 在当前ShutdownMode设置为OnLastWindowClose。
  •   
  • 当ShutdownMode设置为OnMainWindowClose。
  •   
  • 当用户结束会话和SessionEnding事​​件可以是未处理的,或不取消处理。
  •   
  • 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(); 仅可从创建应用程序的线程调用对象,即通常主线程。

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天全站免登陆