MVVM-视图模型-视图模型通信 [英] MVVM- View Model-View Model Communications

查看:22
本文介绍了MVVM-视图模型-视图模型通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用 MVVM Light 让两个视图模型相互通信.我知道如何使用信使类和注册等.这是我的场景

How do I go about having two view models communicate with one another using MVVM Light. I know how to use the messenger class and register etc.. Here is my Scenario

A Settings View ---> a Settings View Model 
                                  .
                                  .
                                  .

A MainPage View ---> A MainPage ViewModel

如果设置视图中的某些内容发生变化,它将向设置视图模型发送消息.那么我希望设置视图模型与 MainPage 视图模型就更改的内容进行通信.MainPage ViewModel 然后会告诉 View.

If something changes in the Settings View it will Message back to the Settings View Model. So then I want the Settings View Model to communicate to the MainPage View Model about what changed. THe MainPage ViewModel will then tell the View.

推荐答案

这种问题的常见模式是 Mediator(两个视图模型都引用的类,可用于在两者之间传递消息).

A common pattern for this style of problem is Mediator (a class that both view models reference and can be used to pass messages between the two).

Mediator 类已移至 Cinch WPF/SL MVVM 框架,它似乎仍然积极开发/支持.

The Mediator class has since been moved to the Cinch WPF/SL MVVM Framework, which appears to still be actively developed/supported.

我更喜欢的模式是事件聚合器,可以在 Prism 框架中找到一个例子.在这种模式中,不同的视图模型订阅来自聚合器的事件,而其他视图模型则发布事件.

The pattern I prefer is the Event Aggregator, an example can be found in the Prism framework. In this pattern different view models subscribe to events from the aggregator and others publish events.

希望能帮到你

这篇关于MVVM-视图模型-视图模型通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆