Lets say I assign one handler to two events like this:
<Canvas PointerPressed="Handler" PointerMoved="Handler"/>
And I have the Handler as:
void Handler(object sender, PointerRoutedEventArgs e) {
/*
*/
}
How do I know from inside Handler which particular event (PointerMoved or PointerPressed) triggered it ?
The second parameter
EventArgsshould have a type corresponding to the particular event the handler is attached to. Therefore, you can determine which event an eventhandler belongs to just by looking at the type of the second parameter.The
EventHandleris in reality a delegate (see this). Your handler method signature has to match (including the type of the parameters) with that delegate's signature,so I don't think you can subcribe toPointerMovedandPointerPressedevents withvoid Handler(object sender, PointerRoutedEventArgs e)in the first place.If you are trying to figure out to which event a particular handler is called from between two
EventHandlersof the same type, then your best bet is to look at the first parametersender. As the name implies, this is a reference to the object that raised the event itself.UPDATE: Sorry, I didn't realize both events do indeed use the same type PointerRoutedEventArgs:
so it's not an option for you to look at the type of the EventArgs argument. As another user said in the comments, it's strange to use the same handler for different events in the first place. In such case, you'd probably need to go case by case, and see what properties of
PointedRouterEventArgsare relevant to each of the possible events. For example, as shown in this answer, you could check the propertiesIsRightButtonPressedandIsLeftButtonPressedofe.GetCurrentPoint(this)to determine if the raised event was aPointerPressedor a different one. And the same idea with other properties in the EventArgs parameter particularly relevant toPointerMoved: