I’m trying to make this extension method. Getting an error on the casts saying ‘Cannot convert type T to MouseEventArgs’. Shouldn’t I be able to upcast the object (since MouseEventArgs : InputEventArgs)?
In case you are curious, I am using this to share event handlers between Mouse and Touch events by just using the base InputEventArgs in the handler method.
public static class InputEventArgsExtensions
{
public static Point GetPosition<T>(this T e, IInputElement obj)
where T : InputEventArgs
{
if (e is MouseEventArgs)
return ((MouseEventArgs)e).GetPosition(obj);
else if (e is TouchEventArgs)
return ((TouchEventArgs)e).GetTouchPoint(obj).Position;
return new Point();
}
}
I tried doing this and it worked for me.