我正在尝试制作这种扩展方法.在演员说“无法将类型T转换为MouseEventArgs”时出错.我不应该能够向上转换对象(因为MouseEventArgs:InputEventArgs)?
如果你很好奇,我使用它来共享Mouse和Touch事件之间的事件处理程序,只需使用处理程序方法中的基本InputEventArgs.
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();
}
}
最佳答案 我试过这样做,它对我有用.
public static class InputEventArgsExtensions
{
public static Point GetPosition<T>(this T e, IInputElement obj)
where T : InputEventArgs
{
if (e is MouseEventArgs)
return (e as MouseEventArgs).GetPosition(obj);
else if (e is TouchEventArgs)
return (e as TouchEventArgs).GetTouchPoint(obj).Position;
return new Point();
}
}