开发者

should I use RoutedEventHandler

What's the difference between thi开发者_开发知识库s two:

_btnAddNew.Click += OnAddNewClick;

 _btnAddNew.Click += new RoutedEventHandler(OnAddNewClick);

Thank you!!


There is no difference ... the first is a shortcut for the second.

In fact, if you try both ways, then use Reflector to disassemble the assembly, you can see that it's exactly the same and both are interpreted as:

_btnAddNew.Click += new RoutedEventHandler(OnAddNewClick);


copying from: http://msdn.microsoft.com/en-us/library/system.windows.routedeventhandler.aspx

The RoutedEventHandler delegate is used for any routed event that does not report event-specific information in the event data. There are many such routed events; prominent examples include Click and Loaded.

The most noteworthy difference between writing a handler for a routed event as opposed to a general common language runtime (CLR) event is that the sender of the event (the element where the handler is attached and invoked) cannot be considered to necessarily be the source of the event. The source is reported as a property in the event data (Source). A difference between sender and Source is the result of the event being routed to different elements, during the traversal of the routed event through an element tree.

You can use either sender or Source for an object reference if you are deliberately not interested in the routing behavior of a direct or bubbling routed event and you only intend to handle routed events on the elements where they are first raised. In this circumstance, sender and Source are the same object.

If you do intend to take advantage of the inherent features of routed events and write your handlers accordingly, the two most important properties of the event data that you will work with when writing event handlers are Source and Handled.

For certain combinations of input events and WPF control classes, the element that raises the event is not the first element that has the opportunity to handle it. If the input event has a Preview version of the event, then the root of the element tree has first opportunity, can set Handled to true in the shared event data, and can influence how the input event is reported to remaining elements in its event route. The Preview handling behavior can give the appearance that a particular routed event is not raised as expected. For more information, see Preview Events and Input Overview.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜