使用AddHandler在功能上与Event + =方法不同吗? [英] Is using AddHandler functionally different from Event += Method?

查看:90
本文介绍了使用AddHandler在功能上与Event + =方法不同吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了两行代码:

I've got two lines of code I've worked with:

textBox_ssn.KeyDown += ButtonSS;

textBox_ssn.AddHandler(Control.KeyDownEvent, new KeyEventHandler(ButtonSS), true);

据我所知,它们的工作方式相同,但是幕后有什么不同之处.

As far as I can tell they both work in the same way, but is there something different going on behind the scenes.

推荐答案

使用AddHandler,您可以为同一事件多次添加同一处理程序,而不会引发异常.但是,处理事件时,实际上会多次调用管理器.

With AddHandler you can add the same handler for the same event multiple times without throwing an exception. However, the manager is actually called several times when the event is handled.

因此,请考虑这种行为如何产生副作用,应在处理程序实现中加以解释.

Therefore, consider how this behavior can have side effects that should be explained in your handler implementation.

这篇关于使用AddHandler在功能上与Event + =方法不同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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