不能获得一个routed event 有多少个event handler

原来的普通eventHandler可以通过

eventHandler clickHandler;

clickHandler.GetInvocationList().Length 来获得

foreach(delegate a in GetclickHandler.GetInvocationList())

{

}

现在的routedEventHandler不能这样,可选择方案:

 it's very hard to count event handlers for a specific element without writing a custom class to do this.  Here's a couple ideas...

 

1) Use a Dictionary to store references to previously created events.  This way, when adding events again, you simply check the dictionary to see if you're adding something that's already defined:

 

Code Snippet

Dictionary<UIElement, RoutedEventHandler> myHandlers = newDictionary<UIElement, RoutedEventHandler>();

Button b = newButton();

 

RoutedEventHandler handler = newRoutedEventHandler(b_Click);

myHandlers[b] = handler;

b.Click +=

newRoutedEventHandler(b_Click);

见:

 http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/54a21c0f-01d8-4204-be5b-df92ef525415/

 

 

 

 

2) Take steps to avoid reentrancy into your code that adds event handlers, such as a "Initialized" boolean value.

posted on 2008-10-20 12:38  oyl  阅读(242)  评论(0编辑  收藏  举报

导航