简体   繁体   English

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

[英]WPF c# Subscribe/Publish events mechanism using reflection - right or wrong?

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. 我想在WPF应用程序中实现一种简单的“订阅/发布”机制,以便我可以从应用程序中不同位置订阅事件,并指定事件类型和处理程序方法,然后在发布事件时,我的机制将调用所有订阅的方法订户。 I cannot use the RoutedEvent as I want to raise events freely without being forced to the visual tree. 我不能使用RoutedEvent,因为我想自由引发事件而不被迫进入可视化树。

I am able to achieve the above using c# reflection. 我可以使用C#反射实现以上目标。 But as I am pretty new to c# and WPF(coming from Java), I want to be sure that this is the preferred way. 但是由于我对C#和WPF(来自Java)还很陌生,所以我想确保这是首选方式。

Is It ?? 是吗 ??

Many Thanks, 非常感谢,

It looks like you want the "Event Aggregator" pattern from the Prism framework. 看起来您想要Prism框架中的“事件聚合器”模式。 Surely you can implement it by yourself, although I don't see why you might need reflection for it. 当然,您可以自己实现它,尽管我不明白为什么您可能需要对此进行反思。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM