WPF C# 使用反射订阅/发布事件机制 - 对还是错?

发布于 2024-09-09 11:08:01 字数 231 浏览 3 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

予囚 2024-09-16 11:08:01

看起来您想要 "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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文