Why isn't my WPF closing event being fired for system closes?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 02:01:25

问题


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?


回答1:


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.




回答2:


Try the Closed event instead: Closed="Window_Closing"



来源:https://stackoverflow.com/questions/3183729/why-isnt-my-wpf-closing-event-being-fired-for-system-closes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!