You can only override OnDeactivated within a class derived from the one declaring it - mere "other" code can only use the event
Within OnDeactived, it's up to you to decide whether or not to call base.OnDeactivated - you could effectively suppress the event, or change the parameters; invoke it before or after your own code, etc.
If you're already deriving from the class, either way would work - personally I'd probably use the event more often than not, unless I wanted to take any other sort of action that could only be performed by overriding. Aside from anything else, that makes the code more portable if you want to move it anywhere else. Then again, I'm generally not a fan of inheritance anyway, so I'm biased :)
protected override void OnDeactivated(EventArgs e)
{
//run some code before execution (anything that could effect execution)
//call the base method and fire the event
base.OnDeactivated(e);
//run some code after execution
}
The decision of whether to override the method or use the event handler often times comes down to how much control you need to have over what happens during the execution of that method. Overriding the method gives you full control of the method, whereas the event handler only runs after the method has executed.
If you need a high level of control over what happens during that method, I would advise overriding the method. If you simply need to run some code after execution of the method, I would use the event handler.
protected override void OnDeactivated(EventArgs e)
{
//run some code before execution (anything that could effect execution)
//call the base method and fire the event
base.OnDeactivated(e);
//run some code after execution
}
发布评论
评论(2)
有两个明显的区别:
OnDeactivated
- 单纯的“其他”代码只能使用该事件OnDeactived
中,它是由您决定是否调用base.OnDeactivated
- 您可以有效地抑制该事件,或更改参数;在您自己的代码之前或之后调用它,等等。如果您已经从类派生,则任何一种方法都可以 - 就我个人而言,我可能会更频繁地使用该事件,除非我想采取任何其他< /em> 此类操作只能通过覆盖来执行。除此之外,如果您想将代码移动到其他地方,这会使代码更可移植。话又说回来,我通常不喜欢继承,所以我有偏见:)
There are two obvious differences:
OnDeactivated
within a class derived from the one declaring it - mere "other" code can only use the eventOnDeactived
, it's up to you to decide whether or not to callbase.OnDeactivated
- you could effectively suppress the event, or change the parameters; invoke it before or after your own code, etc.If you're already deriving from the class, either way would work - personally I'd probably use the event more often than not, unless I wanted to take any other sort of action that could only be performed by overriding. Aside from anything else, that makes the code more portable if you want to move it anywhere else. Then again, I'm generally not a fan of inheritance anyway, so I'm biased :)
是否重写该方法或使用事件处理程序的决定通常取决于您需要对该方法执行期间发生的情况有多少控制。重写该方法使您可以完全控制该方法,而事件处理程序仅在该方法执行后运行。
如果您需要对该方法期间发生的情况进行高度控制,我建议覆盖该方法。如果您只需要在执行方法后运行一些代码,我将使用事件处理程序。
The decision of whether to override the method or use the event handler often times comes down to how much control you need to have over what happens during the execution of that method. Overriding the method gives you full control of the method, whereas the event handler only runs after the method has executed.
If you need a high level of control over what happens during that method, I would advise overriding the method. If you simply need to run some code after execution of the method, I would use the event handler.