c# - 如果 e.Cancel = true,我应该在何时何地调用 base.OnClosing(e)?

标签 c# wpf window

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/

相关文章:

.net - 不同的 Windows XP 风格正在改变我的控件外观,我该如何避免呢?

c# - 如何从 WPF 背后的代码使用 ItemsControl 创建 DataTemplate

c - 如何将对话框置于主程序窗口位置的中心?

c# - 创建 Ackermann Function 程序时防止 System.StackOverflowException

WPF - 绑定(bind) StringFormatting 不起作用

jquery - 如何确定对象到浏览器窗口右边框的距离

dialog - Electron:文件对话框窗口应聚焦直到做出决定

c# - 通知窗口 - 防止窗口获得焦点

C# Linq-to-SQL 创建通用存储库

c# - DirectoryEntry 刷新杂项问题