在MVVM中的视图模型之间进行通信 [英] Communicate between View Models in MVVM

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

问题描述

我在我的项目中使用MVVM模式.这是伪代码.

I am using MVVM pattern in my project. Here is the pseudo code.

class ViewModelA {
     propertyA;
     methodA(); // updates propertyA
}

class ViewModelB {
     methodB() {
     // update properties of ViewModelB
     // Needs to update propertyA of ViewModelA
     }
}

我有两个视图模型ViewModelA和ViewModelB.ViewModelB的MethodB需要更新ViewModelA的propertyA.

I have two view models ViewModelA and ViewModelB. MethodB of ViewModelB needs to update propertyA of ViewModelA.

我可以在ViewModelB内保留ViewModelA实例的引用,并调用methodA来更新ViewModelA的propertyA.在MVVM模式中将一个视图模型的引用保留在另一个视图模型中是一种好习惯吗?另外,想了解View模型更新其他View模型属性的其他方式.

I can keep a reference of an instance of ViewModelA inside ViewModelB and call methodA to update propertyA of ViewModelA. Is it a good practice to keep a reference of one view model inside another view model in MVVM pattern? Also, would like to know other ways in which View models can update properties of other view models.

推荐答案

您可以查看它具有消息传递机制的GalaSoft MVVMLight Toolkit.它可以用于在视图模型之间进行通信: http://dotnetpattern.com/mvvm-light-messenger https://mallibone.com/post/mvvmlight-messenger https://www.nuget.org/packages/MvvmLight/

you can look at GalaSoft MVVMLight Toolkit it has messaging mechanism. it can be used to communicate beetween viewmodels: http://dotnetpattern.com/mvvm-light-messenger, https://mallibone.com/post/mvvmlight-messenger, https://www.nuget.org/packages/MvvmLight/

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

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