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

查看:128
本文介绍了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

如果设置视图中的内容发生变化,那么Message将返回设置视图模型。所以我想要设置视图模型与MainPage视图模型通信有关改变。那么MainPage ViewModel将会告诉我们的观点。

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天全站免登陆