C#/WPF - WPF 类中的 RoutedEvent 不是 UIElement
我有一堂课需要通知发生了一些重要的事情。该类位于 WPF 项目中,尽管这个特定的类看起来没什么(并且不直接或间接继承自 UIElement
)。
通常,我只需注册一个 RoutedEvent 即可获得此功能,但由于此类既没有 AddHandler 也没有 RemoveHandler,所以我无法让它工作。有人知道获取 RoutedEvent 行为的另一种方法吗?
I have a class that needs to notify that something significant has occurred. The class is in a WPF-project, even though this specific class, is lookless (and doesn't inherit from UIElement
, neither directly or indirectly).
Normally, I just register a RoutedEvent
to get this functionality but as this class neither has AddHandler nor RemoveHandler, I can't get it to work. Anyone knows of another way of get the RoutedEvent
behaviour?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,如果你的类不是
UIElement
,它就不能成为可视化树的一部分,如果它不是可视化树的一部分,你就不能抛出RoatedEvent
。它们严格来说是一个 UI 概念。我认为推荐的方法是让您的类继承
UIElement
,或者如果这是不可能/不需要的,则为您的类创建一个从UIElement
继承的对应类,并且在通常放置原始类的可视化树中使用第二个类。As far as I know, if your class isn't a
UIElement
, it cannot be part of the visual tree, and if it isn't part of the visual tree, you cannot throwRoutedEvent
s. They're strictly a UI concept.I think the recommended approach would be to either make your class inherit from
UIElement
, or if that's not possible/desired, create a counterpart for your class which does inherit fromUIElement
and use this second class in the visual tree where you would normally place your original class.