为什么窗口的卸载事件不会在 WPF 中触发? [英] Why does unloaded event of window do not fire in WPF?

查看:27
本文介绍了为什么窗口的卸载事件不会在 WPF 中触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 WPF 应用程序中,我创建了一个窗口,并通过调用 ShowDialog() 方法将其显示为对话框.但是当我通过 Close() 方法关闭窗口时,此对话框窗口不会触发 Unloaded 事件.

In my WPF application I have created a window and show it as a dialog by calling it by the method ShowDialog(). But when I close the window by Close() method the Unloaded event is not fired for this dialog window.

MyWindow obj = new MyWindow(); 
obj.ShowDialog();
obj.Close();

推荐答案

这是一个已知问题.

改用这个

   yourWindow.Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted;

   private void Dispatcher_ShutdownStarted( object sender, EventArgs e )
   {
       //do what you want to do on closing
   }

阅读了解更多详情

编辑

如果以上不起作用,请尝试此

If above is not working try this

yourWindow.Closing += new CancelEventHandler(YourWindow_Closing);

void YourWindow_Closing(object sender, CancelEventArgs e)
{

}

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

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