如何在关闭处理程序中区分“单击窗口关闭按钮(X)"与 window.Close()

编程入门 行业动态 更新时间:2024-10-25 17:23:11
本文介绍了如何在关闭处理程序中区分“单击窗口关闭按钮(X)"与 window.Close()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-25 23:33:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1125519.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   按钮   窗口   程序   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!