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;
}
}
