UIElement.AddHandler()vs .Event + =定义 [英] UIElement.AddHandler() vs .Event += Definition
问题描述
1.部分问题:
这两个事件注册有什么区别?
1.st part of the quesion: What is the difference between these 2 event registrations ?
_popUp.AddHandler(PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(PopUp_PreviewMouseLeftButtonDown));
_popUp.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(_popUp_PreviewMouseLeftButtonDown);
2.nd问题的一部分:
或最终与
2.nd part of the question: or eventually versus
popUp.Opened += PopUp_Opened;
推荐答案
根据 Redgate的反射器,没有区别。两种方法最终都调用内部方法 EventHandlerStore.AddRoutedEventHandler
。这是 add
addor for PreviewMouseLeftButtonDown
事件(在类 UIElement中)的反射器输出
):
According to Redgate's Reflector, there is no difference. Both methods eventually call the internal method EventHandlerStore.AddRoutedEventHandler
. This is the reflector output of the add
accessor for the PreviewMouseLeftButtonDown
event (in the class UIElement
):
public void add_PreviewMouseLeftButtonDown(MouseButtonEventHandler value)
{
this.AddHandler(PreviewMouseLeftButtonDownEvent, value, false);
}
正如你可以看到它调用 UIElement.AddHandler
为您。
As you can see it calls UIElement.AddHandler
for you.
在编辑您的问题之前,您询问有关已开启的
事件弹出窗口在这种情况下,有一个区别:首先,已打开的
事件不是作为路由事件实现的,而是作为一个简单的事件,所以你甚至不能使用 AddHandler
调用它。其次,反射器表明在 EventHandlerStore
中调用了一种不同的方法,它将处理程序添加到简单的委托集合中。
Before you edited your question you were asking about the Opened
event of the popup. In that case, there is a difference: First, the Opened
event is not implemented as a routed event but as a simple event, so you can't even use the AddHandler
call on it. Secondly, the reflector shows that a different method is called in the EventHandlerStore
which adds the handler to a simple delegate collection.
这篇关于UIElement.AddHandler()vs .Event + =定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!