的WinForms订阅自己的事件 [英] Winforms subscribing to own events

查看:105
本文介绍了的WinForms订阅自己的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到开发商的WinForms做一个共同点是订阅他们自己的活动等你拿

One common thing I see developers doing in WinForms is forms/controls subscribing to their own events so you get

this.Load += new System.EventHandler(this.WelcomeQuickViewWF_Load);
this.Activated += new System.EventHandler(this.WelcomeQuickViewWF_Activated);



而不是

rather than

protected override void OnActivated(EventArgs e)
{
}

现在我所知道的第二个方法是面向多个对象,第一种方式是事件驱动的,我往往倾向于压倒一切的重构 - 有什么理由不这样做呢?我不希望做的是让那些不是真正需要改变的,纯粹的审美选择。

Now I know the second way is more Object Oriented, the first way is event driven and I tend to refactor towards overriding - is there any reason NOT to do this? What I don't want to be doing is making changes that are not really needed and purely an aesthetic choice.

推荐答案

通过订阅自己的事件,你放弃的时候的的代码时,调用了控制还有其它订户事件。通过重写事件饲养方法,你何时应该调用你的代码完全控制。你可以通知用户之前或之后调用它。但也没有一个可取消的事件被取消代码的机会。

By subscribing to your own event, you give up control over when your code is invoked when there are other subscribers to the event. By overriding the event raising method, you have full control over when your code should be invoked. You could invoke it before notifying subscribers or after. There's also no chance of your code being canceled on a cancelable event.

如果该方法的重写,这样做。否则,注册为你没有其他的选择。

If the method is overridable, do so. Otherwise register as you have no other choice.

这篇关于的WinForms订阅自己的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆