如何在关闭处理程序中区分“单击窗口关闭按钮(X)"与 window.Close() [英] How to distinguish 'Window close button clicked (X)' vs. window.Close() in closing handler

查看:28
本文介绍了如何在关闭处理程序中区分“单击窗口关闭按钮(X)"与 window.Close()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种聪明的方法来检测窗口是否被关闭

Is there a clever way to detect whether a window was closed by

  • 用户按下窗口右上角的 (X) 按钮或
  • window.Close() 已以编程方式调用.

我想在 window.Closing 处理程序中检测到这一点.我可以在每次调用 window.Close() 时设置一个标志,但这不是一个很好的解决方案.

I would like to detect this in the window.Closing handler. I could set a flag whenever I call window.Close(), but this is not a very pretty solution.

推荐答案

我不确定我是否喜欢这个问题,但您显然有理由提出这个问题.如果您要在 OnClosing 事件中进行堆栈跟踪,您可以查找 Window.Close 事件.

I'm not sure I like this at all but it's a question that you obviously have a reason for asking. if you were to take a stack trace in the OnClosing event you could look up for the Window.Close event.

protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
   bool wasCodeClosed = new StackTrace().GetFrames().FirstOrDefault(x => x.GetMethod() == typeof(Window).GetMethod("Close")) != null;
   if (wasCodeClosed)
   {
       // Closed with this.Close()
   }
   else
   {
       // Closed some other way.
   }

   base.OnClosing(e);
}

这篇关于如何在关闭处理程序中区分“单击窗口关闭按钮(X)"与 window.Close()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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