OnLoad方法和Load事件之间的区别?

时间:2021-11-03 20:12:11

What is the difference between OnLoad method and Load event? I am developing WinForm controls. Should I register to Load event or override the OnLoad method? What are the advantages and the disadvantages of each one?

OnLoad方法和Load事件有什么区别?我正在开发WinForm控件。我应该注册Load事件还是覆盖OnLoad方法?每个人的优点和缺点是什么?

3 个解决方案

#1


I'd go for overriding OnLoad, so you spare the CPU cycles to invoke the event handler.

我会去覆盖OnLoad,所以你不用CPU周期来调用事件处理程序。

The general pattern is to override a method, if you inherit from a control; otherwise, subscribe to the event.

一般模式是覆盖一个方法,如果你从一个控件继承;否则,订阅该活动。

But remember to call the base class' OnLoad method, because that's where the Load event invoked.

但是请记住调用基类的OnLoad方法,因为这是调用Load事件的地方。

#2


OnLoad method is the one that raises Load event. It's a standard pattern in framework classes, and a generally recommended one - for any event Foo, you have a virtual protected method OnFoo which raises that event; and no other method of the class raises the event directly, but always calls OnFoo.

OnLoad方法是引发Load事件的方法。它是框架类中的标准模式,通常建议使用 - 对于任何事件Foo,您都有一个虚拟保护方法OnFoo,它会引发该事件;并且该类的其他方法没有直接引发事件,但总是调用OnFoo。

If you need to handle the event on this, it's usually both easier and faster to override OnFoo.

如果你需要处理这个事件,那么覆盖OnFoo通常会更容易,更快。

#3


OnLoad is the default event handler used in VB.NET to handle the Load event. I typically override this method when I need to attach code to the load event. There are also default functions for the other Page Life Cycle events: OnPreRender, OnInit, etc.

OnLoad是VB.NET中用于处理Load事件的默认事件处理程序。当我需要将代码附加到load事件时,我通常会覆盖此方法。其他页面生命周期事件也有默认功能:OnPreRender,OnInit等。

#1


I'd go for overriding OnLoad, so you spare the CPU cycles to invoke the event handler.

我会去覆盖OnLoad,所以你不用CPU周期来调用事件处理程序。

The general pattern is to override a method, if you inherit from a control; otherwise, subscribe to the event.

一般模式是覆盖一个方法,如果你从一个控件继承;否则,订阅该活动。

But remember to call the base class' OnLoad method, because that's where the Load event invoked.

但是请记住调用基类的OnLoad方法,因为这是调用Load事件的地方。

#2


OnLoad method is the one that raises Load event. It's a standard pattern in framework classes, and a generally recommended one - for any event Foo, you have a virtual protected method OnFoo which raises that event; and no other method of the class raises the event directly, but always calls OnFoo.

OnLoad方法是引发Load事件的方法。它是框架类中的标准模式,通常建议使用 - 对于任何事件Foo,您都有一个虚拟保护方法OnFoo,它会引发该事件;并且该类的其他方法没有直接引发事件,但总是调用OnFoo。

If you need to handle the event on this, it's usually both easier and faster to override OnFoo.

如果你需要处理这个事件,那么覆盖OnFoo通常会更容易,更快。

#3


OnLoad is the default event handler used in VB.NET to handle the Load event. I typically override this method when I need to attach code to the load event. There are also default functions for the other Page Life Cycle events: OnPreRender, OnInit, etc.

OnLoad是VB.NET中用于处理Load事件的默认事件处理程序。当我需要将代码附加到load事件时,我通常会覆盖此方法。其他页面生命周期事件也有默认功能:OnPreRender,OnInit等。