WPF C# 使用反射订阅/发布事件机制 - 对还是错?
我想在 WPF 应用程序中实现一个简单的订阅/发布机制,这样我就可以从应用程序中的不同位置订阅事件,指定事件类型和处理程序方法,然后在发布事件时,我的机制将调用所有订阅的方法订户。我无法使用 RoutedEvent,因为我想自由地引发事件而不被迫使用可视化树。
我能够使用 C# 反射来实现上述目标。但由于我对 C# 和 WPF(来自 Java)还很陌生,我想确保这是首选方式。
是吗??
非常感谢,
I want to achieve a simple Subscribe/Publish mechanism within a WPF application so i can subscribe to events from different places in the application specifying the event type and handler method, and then when publishing the event, my mechanism will call all the subscribed methods on the subscribers. I cannot use the RoutedEvent as I want to raise events freely without being forced to the visual tree.
I am able to achieve the above using c# reflection. But as I am pretty new to c# and WPF(coming from Java), I want to be sure that this is the preferred way.
Is It ??
Many Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您想要 "Event Aggregator" 模式 <一个 href="http://compositewpf.codeplex.com/" rel="nofollow noreferrer">Prism 框架。当然你可以自己实现它,尽管我不明白为什么你可能需要反思它。
It looks like you want the "Event Aggregator" pattern from the Prism framework. Surely you can implement it by yourself, although I don't see why you might need reflection for it.