如何在.Net WPF MVVM应用程序的ViewModel之间传递事件? [英] How do I communicate events between ViewModels in a .Net WPF MVVM application?
问题描述
我正在创建一个.Net WPF应用程序,它是一个仪表板.我需要能够在仪表板上的不同视图模型之间交流事件的能力.在我看来,这就像一个发布/订阅事件模型.
I am creating a .Net WPF application that is a dashboard. I need the ability to communicate events between different View Models on the dashboard. This to me feels like a pub/sub events model.
实现与MVVM FrameWork相适应的解决方案的最佳方法是什么?我开始看棱镜,但是想知道这对我的需求是否有点麻烦.
What is the best way to implement a solution which fits well with the MVVM FrameWork? I started to look at Prism, but am wondering if that is a little heavy handed for my needs.
有人可以推荐最佳实践方法,并向我指出一些简单的方法吗?实施示例?
Can someone recommend a best practices approach and point me to some simple examples of implementation?
谢谢,约翰·B
推荐答案
请参考以下博客文章,了解如何使用事件聚合器模式以松散耦合的方式在视图模型之间进行通信:
Please refer to the following blog post about how to use the event aggregator pattern to communicate between view models in a loosely coupled fashion: https://blog.magnusmontin.net/2014/02/28/using-the-event-aggregator-pattern-to-communicate-between-view-models/.
这应该回答您的问题.
Another option is to use a shared service: https://social.msdn.microsoft.com/Forums/en-US/22907a0f-d805-4195-8272-7c284b72d2ee/example-of-using-shared-services-prism?forum=wpf
在GitHub上最新版本的Prism中,有一个示例如何使用 EventAggregator
类:
There is an example of how to use the EventAggregator
class in the latest version of Prism available on GitHub: https://github.com/PrismLibrary/Prism-Samples-Wpf/tree/master/EventAggregation
这篇关于如何在.Net WPF MVVM应用程序的ViewModel之间传递事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!