问题
I'm trying to register handler for all Loaded events:
EventManager.RegisterClassHandler(typeof(UIElement), FrameworkElement.LoadedEvent, new RoutedEventHandler(OnRoutedEvent), true);
EventManager.RegisterClassHandler(typeof(ContentElement), FrameworkContentElement.LoadedEvent, new RoutedEventHandler(OnRoutedEvent), true);
EventManager.RegisterClassHandler(typeof(UIElement), FrameworkElement.UnloadedEvent, new RoutedEventHandler(OnRoutedEvent), true);
EventManager.RegisterClassHandler(typeof(ContentElement), FrameworkContentElement.UnloadedEvent, new RoutedEventHandler(OnRoutedEvent), true);
Unfortunately my handler is only called for Window and descendants of DatePicker. It does not work for any other type.
Can anyone explain this behavior ? It's a bug, sadly they've chosen to ignore it.
Is there some other way to be notified about any new element in application/window ?
回答1:
Perhaps you could define an AttachedProperty/Behavior<T>
that uses FrameworkPropertyMetadataOptions.Inherits
, and then specify it on your root element, and in the behaviour hook the Loaded/Unloaded
events on each instance
来源:https://stackoverflow.com/questions/11455800/routed-event-class-handler-for-loadedevent-does-not-work-for-most-classes