(Image source : http://themetapicture.com/definition-of-programmer/ )
這兩天稍微看了一下 Silverlight 中有關 RoutedEvent 的部份
稍微記一下其中幾個重要的地方,才不會忘記:
1. 什麼是 RoutedEvent ?
所謂的 RountedEvent 指的是當某一個 Children 物件觸發了某個 RoutedEvent 後
這種類型的event會將其一直往上傳給所有包含此物件的 Parent物件
直到傳遞到 root element為止 ( bubbling 的概念)
MSDN Says:
> Events in Silverlight follow the "bubbling" routing strategy(for routed event) or do not route at all.
> Events in WPF supports the "tunneling" routing strategy.
2. Sender 與 OriginalSource 的不同 :
RoutedEventHandler 中的 Sender 跟 OriginalSource是不太一樣的
Sender 指的是這個 Routed事件是附加在哪一個Object上頭
而當某個物件觸發了 RoutedEvent 時,我們通常感興趣的是最初是哪個Child觸發了這個事件
為了取得這個物件 我們要用的是OriginalSource 而非 Sender
(想像OriginalSource為整條Route的最開頭 也就是最初觸發的那個物件)
3. 在Silverlight中,有些 RoutedEvent 支援 Handler 屬性。
如果將Handled屬性設為True, 則此事件將不會繼續往上傳遞給下一個Parent物件
MSDN Says: If you set Handled to true, then the event will generally not be routed to the next object along the event route.
如果上述觀念有誤還請前輩們多多指教!
Reference:
1. http://msdn.microsoft.com/en-us/library/cc189018(vs.95).aspx
2. http://msdn.microsoft.com/en-us/library/system.windows.routedeventhandler(v=vs.95).aspx
留言列表