C# 使用事件处理程序与覆盖事件触发方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/657685/
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-08-04 12:09:04  来源:igfitidea点击:

Using event handlers vs overriding event-firing methods

c#.netwinforms

提问by Jeremy

I am creating a subclass of Button and would like to add custom functionality to some of its events such as OnClick. Which is the more desirable way to do it? Do I override OnClick:

我正在创建 Button 的子类,并希望向其某些事件(例如 OnClick)添加自定义功能。哪种方法更理想?我是否覆盖 OnClick:

protected override void OnClick(EventArgs e)
{
    base.OnClick(e);
    doStuff();
}

or should I instead link up the OnClick event to an event handler defined in my Button subclass through the designer?

或者我应该通过设计器将 OnClick 事件链接到我的 Button 子类中定义的事件处理程序?

class ButtonSubclass
{
    public ButtonSubclass() : base()
    {
        InitializeComponent();
    }

    private void InitializeComponent()
    {
        this.Click += new System.EventHandler(this.ButtonSubclass_Click);
    }
}

Edit: I added minor visual changes (that may pass as rudimentary skinning) but most of the changes are in event handlers that I don't want to re-implement (copy-paste) on every form that reuses it.

编辑:我添加了微小的视觉变化(可能作为基本的皮肤传递),但大多数变化都在事件处理程序中,我不想在每个重复使用它的表单上重新实现(复制粘贴)。

采纳答案by Jon Skeet

If you're genuinely specializing the button, it makes sense to override OnClick. If you're only actuallychanging what happens when a button is clicked, I wouldn't subclass Buttonin the first place - I'd justadd event handlers.

如果您真的是专门设计按钮,则覆盖OnClick. 如果您只是实际更改单击按钮时发生的情况,则我不会Button首先进行子类化- 我只会添加事件处理程序。

EDIT: Just to give a bit more of an idea - if you want to add similar event handlers for multiple buttons, it's easy enough to write a utility method to do that, and call it from multiple places. It doesn't require actual subclassing. That's not to say subclass isdefinitely wrong in your case, of course - just giving you extra options :)

编辑:只是为了提供更多的想法 - 如果您想为多个按钮添加类似的事件处理程序,编写一个实用程序方法来做到这一点很容易,并从多个地方调用它。它不需要实际的子类化。当然,这并不是说子类在您的情况下绝对错误的 - 只是给您额外的选择:)

回答by Jakob Christensen

Always override OnClick when inheriting. It gives you better performance.

继承时始终覆盖 OnClick。它为您提供更好的性能。

回答by Francis Gilbert

For a button click, I'd keep it as a method for reading. But I guess it is a personal preference thing. I'd be interested to see what the concensus is.

对于按钮单击,我将其保留为一种阅读方法。但我想这是个人喜好的事情。我很想知道共识是什么。

回答by Michael Yutsis

Actually, there is one advantage of attaching an event handler over overriding a method. Your event handler is private, while your overridden method has to be protectedwith a predefined name. If you use obfuscating/encrypting tools to protect your application, your code is more exposed when it's protected overridethan when private.

实际上,附加事件处理程序覆盖覆盖方法有一个优点。您的事件处理程序是private,而您的重写方法必须使用预定义的名称进行保护。如果您使用混淆/加密工具来保护您的应用程序,那么您的代码protected overrideprivate.