LoadedEvent的路由事件类处理程序不适用于大多数类 [英] Routed event class handler for LoadedEvent does not work for most classes

查看:130
本文介绍了LoadedEvent的路由事件类处理程序不适用于大多数类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为所有Loaded事件注册处理程序:

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);

不幸的是,我的处理程序仅被Window和DatePicker的后代调用.它不适用于任何其他类型.

Unfortunately my handler is only called for Window and descendants of DatePicker. It does not work for any other type.

任何人都可以解释此行为吗?这是 bug 时,可悲的是他们选择忽略它.

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 ?

推荐答案

也许您可以定义一个使用FrameworkPropertyMetadataOptions.InheritsAttachedProperty/Behavior<T>,然后在您的根元素上指定它,并在行为上挂钩Loaded/Unloaded事件在每个实例上

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

这篇关于LoadedEvent的路由事件类处理程序不适用于大多数类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆