Routed event class handler for LoadedEvent does not work for most classes

不羁岁月 提交于 2019-12-10 17:16:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!