如何知道窗口是否被“x"关闭?按钮? [英] How to know whether window was closed by "x" button?

查看:31
本文介绍了如何知道窗口是否被“x"关闭?按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道怎么判断wpf窗口是否被x"按钮关闭了?

Does anyone knows how to find out whether wpf window was closed by "x" button?

推荐答案

最简单的方法(在我看来)是存储一个布尔值,指示用户是否通过其他方法关闭了表单.

The simplest way (in my opinion) is to store a boolean indicating if a user has closed the form via another method(s).

然后在 OnClosing 事件中,检查布尔值是否为 false(表示 x 按钮被点击).

Then in the OnClosing event, do a check to see if the boolean is false (indicating that the x button was clicked).

唯一的问题是您自己设置了布尔值.这是否可能取决于用户关闭表单的其他方式.

The only issue with this is the fact you have set the boolean youself. Wether this is possible is dependant on the other way the user could close your form.

我应该指出,这高度依赖于关闭表单的其他方式.如果您有许多方法可以通过调用 Window.Close() 关闭此窗口,我会考虑创建一个名为 UserClose() 的方法,它为您进行布尔设置.

I should point out that this is highly dependant on the other ways the form can be closed. If you have a number of methods that close this window by calling Window.Close(), i would consider creating a single method called UserClose(), which does the boolean setting for you.

public void UserClose()
{
    NonXClose = true;
    this.Close();
}

这将允许外部代码关闭窗口,设置布尔值.

This will allow outside code to close the window, with the setting of the boolean.

这篇关于如何知道窗口是否被“x"关闭?按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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