wpf 我应该使用 Closing 事件还是覆盖 OnClosing?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20954051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:25:57  来源:igfitidea点击:

Should I use the Closing event or override OnClosing?

c#.netwpf

提问by user2023861

I have a WPF project in which I have a window with custom Close logic. I want some code to run when a user closes a window. I know of two ways to do this and I'm wondering which is better:

我有一个 WPF 项目,其中有一个带有自定义关闭逻辑的窗口。我想要一些代码在用户关闭窗口时运行。我知道有两种方法可以做到这一点,我想知道哪种更好:

Option 1) Handle the base.Closing event.

选项 1) 处理 base.Closing 事件。

Option 2) Override the OnClosing method.

选项 2) 覆盖 OnClosing 方法。

Here's some sample code:

这是一些示例代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        base.Closing += this.MainWindow_Closing;
    }

    //Option 1
    void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        //close logic here, or
    }

    //Option 2
    protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
    {
        //close logic here

        base.OnClosing(e);
    }
}

The only difference I can find between the two options is cosmetic. I like Option 2 better because it just looks cleaner to me. I prefer overriding methods to handling events.

我能找到的两个选项之间的唯一区别是外观。我更喜欢选项 2,因为它对我来说看起来更干净。我更喜欢覆盖方法来处理事件。

Are there any other differences between these two options? I know that Option 1 is provided for some other class to handle this window's Closing event.

这两个选项之间还有其他区别吗?我知道为其他一些类提供了选项 1 来处理此窗口的 Closing 事件。

Edit: I forgot to mention that I'm using .Net 4.0. It looks like .Net 4.5 has an OnFormClosing event that deprecates the OnClosing event. I have not used the OnFormClosing event.

编辑:我忘了提及我正在使用 .Net 4.0。看起来 .Net 4.5 有一个弃用 OnClosing 事件的 OnFormClosing 事件。我没有使用 OnFormClosing 事件。

回答by Mike Perrenoud

The major difference is with the override you determine when the base code is called. When consuming the event, you have no control over that. When consuming the event, you are in essence, the base code.

主要区别在于您确定何时调用基本代码的覆盖。在使用事件时,您无法控制它。在使用事件时,您本质上是基本代码。

This is an important difference at times because if there are consumers of the event but you need to do work before they get called, then you need the override.

这有时是一个重要的区别,因为如果有事件的消费者,但您需要在他们被调用之前做一些工作,那么您需要覆盖。

In this case--probably not a big difference.

在这种情况下——可能差别不大。

回答by Hans Passant

A class listening to its ownevents is a bit silly. It is however the way designers work, they were written to generate the event handler assignment instead of auto-generating the method override. Since you are not using a designer and writing the event handler assignment yourself, you should definitely favor the override. One less thing you can do wrong, you can't forget to write the assignment.

一个听自己事件的类有点傻。然而,这是设计人员的工作方式,他们被编写为生成事件处理程序分配,而不是自动生成方法覆盖。由于您没有使用设计器并自己编写事件处理程序分配,因此您绝对应该支持覆盖。你不能做错的另一件事是,你不能忘记写作业。

It only truly matters if you (or another programmer) derivesa class from your MainWindow class. Now you have a choice on how to write your OnClosing() method:

只有当您(或其他程序员)从 MainWindow 类派生类时才真正重要。现在您可以选择如何编写 OnClosing() 方法:

  • add your custom code beforethe base.OnClosing() call. You allow the derived class to change the decision you made. In other words, for this event the programmer could force e.Cancel back to false. This is the normal way.

  • call base.OnClosing() first, then add your custom code. That puts you firmly in control with no option for the derived class to override your decision. You do this when your decision matters most and/or a derived class cannot possibly override your choice correctly, perhaps because only you have access to private info.

  • not call base.OnClosing(). This prevents the derived code from seeing the event at all. You'd do this when you drastically change the event handling. For this event for example when you cancel the close and, say, hide the window.

  • 在 base.OnClosing() 调用之前添加您的自定义代码。您允许派生类更改您所做的决定。换句话说,对于这个事件,程序员可以强制 e.Cancel 返回false。这是正常的方式。

  • 首先调用 base.OnClosing(),然后添加您的自定义代码。这使您牢牢掌握控制权,派生类无法选择覆盖您的决定。当您的决定最重要和/或派生类可能无法正确覆盖您的选择时,您会这样做,这可能是因为只有您可以访问私人信息。

  • 不调用 base.OnClosing()。这完全阻止了派生代码看到事件。当您彻底改变事件处理时,您会这样做。对于此事件,例如当您取消关闭并隐藏窗口时。

回答by Kyrylo M

When you override OnClosingyou are able to implement the logic for cancelling closing (see MSDN) using CancelEventArgs. See also an example.

当你重写OnClosing你能够实现取消关闭(见逻辑MSDN)使用CancelEventArgs。另请参见示例

In case of event you just do some work before window will be closed and you can't affect the closing process.

万一你只是在窗口关闭之前做一些工作,你不能影响关闭过程。