protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e); // here?
if (cancelCondition)
{
base.OnClosing(e); // or here?
e.Cancel = true;
base.OnClosing(e); // or here?
}
base.OnClosing(e); // or here?
}
我尝试了几个不同的地方,它似乎在任何地方都有效,只是想知道它是否重要。 base.OnClosing(e);
实际上做了什么吗?
最佳答案
来自 MSDN :
A type that derives from Window may override OnClosing. The overridden method must call OnClosing on the base class if Closing needs to be raised
在您的情况下,似乎没有必要实际提高 Closing
事件,因此无论您是否以及在何处调用 base.OnClosing
都无关紧要.
但是,如果您不覆盖 OnClosing
,您可以避免首先决定这一点。方法,但只需添加一个 Closing
处理程序代替:
<Window ... Closing="Window_Closing">
...
</Window>
private void Window_Closing(object sender, CancelEventArgs e)
{
if (cancelCondition)
{
e.Cancel = true;
}
}
关于c# - 如果 e.Cancel = true,我应该在何时何地调用 base.OnClosing(e)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19985948/