简体   繁体   English

使用 Prism for UWP App 在松散耦合组件之间进行通信

[英]Communication between loosely coupled Components using Prism for UWP App

I am new to Prism and have been looking into designing a UWP App which would have 2 XAML views.我是 Prism 的新手,一直在研究设计一个具有 2 个 XAML 视图的 UWP 应用程序。 These 2 views would be loosely coupled and i want to pass messages between the 2 views.这两个视图将松散耦合,我想在两个视图之间传递消息。 One view would have a button and on clicking on this button, a message would be sent to the other view which would highlight and item in the list in that view.一个视图将有一个按钮,单击此按钮后,将向另一个视图发送一条消息,该视图将突出显示该视图中列表中的项目。 I am reading up on the Prism documentation and have some confusion about what would be the best form of communication?我正在阅读 Prism 文档,但对什么是最好的交流方式有些困惑? In particular would my case benefit from Solution Commanding or using the IEvent Aggregator.我的案例尤其会受益于解决方案命令或使用 IEvent 聚合器。 Referencing this post Communicating across modules with Prism?参考这篇文章使用 Prism 跨模块通信? i would love to understand why IEeventAggregator is a good solution我很想了解为什么 IEeventAggregator 是一个很好的解决方案

The purpose of using an event aggregator is to remove the tight copuling between the producer and consumer of an event or a message.使用事件聚合器的目的是消除事件或消息的生产者和消费者之间的紧密耦合。

If you want to send a message from one component to another in your application, you can do this by raising an event or calling a method of a strong reference to the consumer.如果您想从应用程序中的一个组件向另一个组件发送消息,您可以通过引发事件或调用对使用者具有强引用的方法来实现。 The downside of doing this is that you create a strong dependency between the subscriber and publisher classes and this makes the application harder and more expensive to maintain.这样做的缺点是您在订阅者和发布者类之间创建了强依赖关系,这使得应用程序维护起来更加困难且成本更高。

The solution is to introduce an event aggregator in between the publisher and subscriber.解决方案是在发布者和订阅者之间引入一个事件聚合器。 Then the subscriber and the publisher only know about the event aggregator.然后订阅者和发布者只知道事件聚合器。 They don't know anything about each other which means that they can evolve independently from one another.他们对彼此一无所知,这意味着他们可以相互独立地进化。

Please refer to this blog post for more information about the concept.有关该概念的更多信息,请参阅此博客文章

Edit: To answer your actual question, solution commanding is generally used when there is an expectation of immediate action from the user interaction whereas event aggregation is used when there is not a direct action-reaction expectation.编辑:为了回答您的实际问题,通常在期望用户交互中立即采取行动时使用解决方案命令,而在没有直接行动反应期望时使用事件聚合

Please refer to the docs for more information.请参阅文档以获取更多信息。

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

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