Use RoutedEventHandler

What is the difference between the two:

_btnAddNew.Click += OnAddNewClick; _btnAddNew.Click += new RoutedEventHandler(OnAddNewClick); 

Thanks!

+7
c # event-handling
source share
2 answers

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

In fact, if you try in both directions, use Reflector to dismantle the assembly, you can see that it is exactly the same, and both are interpreted as:

 _btnAddNew.Click += new RoutedEventHandler(OnAddNewClick); 
+10
source share

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

The RoutedEventHandler divider is used for any routable event that does not report event information in the event data. There are many such disparate events; Outstanding examples include Click and Loaded.

The most remarkable difference between writing a handler for a routed event, as opposed to a common CLR runtime (CLR) event, is that the event sender (the element to which the handler is attached and called) cannot be considered to be the source of the event. The source is reported as a property in the event data (Source). The difference between the sender and the source is the result of the event being routed to different elements while traversing the routed event through the element tree.

You can use either the sender or the source to refer to the object if you are not intentionally interested in the routing behavior of a direct or spoofed routed event, and you are only going to process routed events on the elements where they are first raised. In this case, the sender and the source are the same object.

If you intend to use the built-in functions 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 ability to handle it. If the input event has a preview version of the event, then the root of the element tree has the first opportunity, can set Handled to true in the data of general events and can affect how the reported event will be transmitted to the remaining elements in its event route. The behavior of the preview processing can lead to to the fact that a particular routed event will not rise as expected. For more information, see Event Preview and Input Overview.

0
source share

All Articles