为什么系统关闭时我的WPF关闭事件不会被触发? [英] Why isn't my WPF closing event being fired for system closes?

查看:766
本文介绍了为什么系统关闭时我的WPF关闭事件不会被触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在XAML文件的窗口节中有以下一行:

  Closing =Window_Closing

我原来以为网页这里向我保证,当您使用大X时,此事件会触发关闭方法, Alt-F4 关闭从系统菜单,但不似乎是这样,因为我的断点在 Window_Closing()函数没有被击中。



它<当我执行文件退出退出方法时,em> / 会触发断点,以便工作正常。



重新阅读链接页面让我相信它可能不会触发结束事件。



我的问题是:


  1. 列出的方法是为了检测您的文件是否脏,需要保存?我有所有的脏和文件保存代码完成,我只需要知道如何陷阱事件。


  2. 你可以停止出口发生使用该方法(可以通过截取关闭事件)?换句话说,如果用户说他们不想退出,因为在错误的窗口上意外使用了 Alt-F4 ,可以做吗?



解决方案

根据关闭事件:


如果会话结束,因为用户记录
关闭或关闭,Closing不是
raise;处理SessionEnding到
实现代码取消
应用程序关闭。


因此,你需要确保你处理 SessionEnding 事件以及结束事件。 SessionEnding事件可用于自动将当前状态保存到临时文件,该文件将在下次应用程序启动时再次加载。但是,如果您想提示用户,可以使用SessionEnding中的模态对话框来实现,但是可能会看到Windows屏幕警告无响应的应用程序,使他们有机会在不响应对话框的情况下终止进程。 / p>

I have the following line in my Window stanza in the XAML file:

    Closing="Window_Closing"

I originally thought that the web page here assured me that this event fires when you use the big X close method, Alt-F4 or close from the system menu yet that doesn't appear to be the case since my breakpoint on the Window_Closing() function isn't being hit.

It does hit the breakpoint when I do the File, Exit method of exiting so that's working okay.

Re-reading that linked page leads me to believe that it may not trigger the closing event.

My questions are:

  1. How do you catch the three methods listed in order to detect if your file is dirty and needs saving? I have all the 'dirty' and file saving code done, I just need to know how to trap the events.

  2. Can you stop the exit from taking place with that method (as you can by intercepting the closing event)? In other words, if the user says they don't want to exit because the accidentally used Alt-F4 on the wrong window, can it be done?

解决方案

According to the documentation page for the Closing event:

If a session ends because a user logs off or shuts down, Closing is not raised; handle SessionEnding to implement code that cancels application closure.

Therefore you'll want to make sure you handle the SessionEnding event as well as the Closing event. The SessionEnding event could be used to automatically save the current state to a temporary file that will be loaded again the next time the application starts. But if you do want to prompt the user, you can do so with a modal dialog box in SessionEnding but they will likely see the Windows screen that warns about unresponsive applications, giving them the chance to kill the process without responding to your dialog.

这篇关于为什么系统关闭时我的WPF关闭事件不会被触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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