2013-05-12 51 views
-2

當我們使用GetType(RoutedEventHandler) 或GetType(RoutedEventArgs)註冊一個自定義路由事件時,有什麼區別?RoutedEventHandler vs RoutedEventArgs

EX1:

Public Shared ReadOnly TextChangedEvent As RoutedEvent = EventManager.RegisterRoutedEvent("TextChanged", _ 
       RoutingStrategy.Bubble, GetType(RoutedEventArgs), GetType(MyCustomTextBlock)) 

EX2:

Public Shared ReadOnly TextChangedEvent As RoutedEvent = EventManager.RegisterRoutedEvent("TextChanged", _ 
       RoutingStrategy.Bubble, GetType(RoutedEventHandler), GetType(MyCustomTextBlock)) 
+0

有人學習WPF可能希望知道爲什麼這個問題被否決沒有這麼多評論。什麼是OP的錯誤。那些在黑暗中將保持在黑暗中。 – Tim 2013-05-12 12:19:53

+0

我知道必須是一個委託類型,但是讓我問的是因爲我有一本書(Microsoft Windows應用程序開發.Net Framework 4)和它使用RoutedEventArgs的示例,它工作正常.... – apollon 2013-05-12 23:04:19

回答

0

當您註冊RoutedEvent,你必須提供一些參數,包括委託類型。

從MSDN

public static RoutedEvent RegisterRoutedEvent(
     string name, 
     RoutingStrategy routingStrategy, 
     Type handlerType, // see note below. 
     Type ownerType) 

handlerType

類型:System.Type

類型的事件處理程序。這必須是委託類型,不能爲空。


選擇類型

因此,第一個實施例(EX1)是使用了不正確的類型。你不會傳遞一個EventArgs。相反,你傳遞一個委託類型。在.NET中有許多委託類型,但在這種情況下,您應該使用從RoutedEvent參數派生的類型。

+0

感謝您的響應。是的,我認爲RoutedEventHandler是正確的答案...但是,因爲我有一本書(Microsoft Windows應用程序開發.Net Framework 4),並使用RoutedEventArgs的例子,它工作正常.... – apollon 2013-05-12 22:20:34

相關問題