UIElement.AddHandler()VS .Event + =定义 [英] UIElement.AddHandler() vs .Event += Definition
问题描述
在quesion的 1.st部分:
是什么这两个事件注册的区别
_popUp.AddHandler(PreviewMouseLeftButtonDownEvent,新MouseButtonEventHandler(PopUp_PreviewMouseLeftButtonDown));
_popUp.PreviewMouseLeftButtonDown + =新MouseButtonEventHandler(_popUp_PreviewMouseLeftButtonDown);
的问题2.nd部分:
或最终与
popUp.Opened + = PopUp_Opened;
据的Redgate's反射,没有任何区别。这两种方法最终调用内部方法 EventHandlerStore.AddRoutedEventHandler
。这是添加
访问的的PreviewMouseLeftButtonDown
事件(在类 UIElement的反射输出
):
公共无效add_PreviewMouseLeftButtonDown(MouseButtonEventHandler值)
{
本.AddHandler(PreviewMouseLeftButtonDownEvent,价值,FALSE);
}
正如你可以看到它调用 UIElement.AddHandler
为您服务。
在你编辑你的问题你问的打开
事件弹出。在这种情况下,是有区别的:首先,打开
事件不作为路由事件,但作为一个简单的事件,所以你甚至不能使用的AddHandler
就可以了电话。其次,反射器显示一个不同的方法是所谓的 EventHandlerStore
它增加了处理程序,以一个简单的委托集合。
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 part of the question: or eventually versus
popUp.Opened += PopUp_Opened;
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);
}
As you can see it calls UIElement.AddHandler
for you.
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屋!