我試圖使這種擴展方法。在演員表示'不能將T型轉換爲MouseEventArgs'時出錯。我不應該能夠上傳對象(因爲MouseEventArgs : InputEventArgs
)?無法投入InputEventArgs MouseEventArgs
如果您好奇,我正在使用它來通過在處理程序方法中使用基本InputEventArgs來共享Mouse和Touch事件之間的事件處理程序。
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();
}
}
啊那好。不幸的是,名稱選擇不當並不能解決問題。 –
對不起;是的,我意識到這不是一個問題後刪除了評論。 –
看看這個鏈接:http://stackoverflow.com/questions/183923/compiler-fails-converting-a-constrained-generic-type – itsme86