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

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

问题描述

几年来,我一直在使用C#(Windows窗体),但从未使用过WPF。但是,现在我喜欢WPF,但是当用户单击文件菜单中的退出菜单项时,我不知道应该如何退出应用程序。

In the few years I've been using C# (Windows Forms), 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();

还有许多其他功能。

推荐答案

要退出应用程序,您可以调用

To exit your application you can call

System.Windows.Application.Current.Shutdown();

Application.Shutdown 方法,您还可以通过指定以下内容来修改应用程序的关闭行为关机模式


在以下情况下Windows Presentation Foundation(WPF)隐式调用关闭:

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


  • 当ShutdownMode设置为OnLastWindowClose时。

  • 当ShutdownMode设置为OnMainWindowClose时。

  • 当用户结束会话和SessionEnding时事件未处理或未经处理就被取消。

请注意, Application.Current.Shutdown(); 只能从创建 Ap的线程中调用对象,即通常是主线程。

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