简体   繁体   English

使用mvvm-light消息传递在非单视图模型的多个实例之间进行通信?

[英]Using mvvm-light messaging to communicate between multiple instances of non-singleton viewmodels?

I have a Parent View with corresponding viewmodel. 我有一个父视图和相应的视图模型。 That Parent view contains child controls with their own viewmodels. 该父视图包含具有其自己的视图模型的子控件。

In the past, with this Parent-Child type relationship, I've usually had the Parent viewmodel contain references to the child viewmodel, and then the child communicates with the parent via events. 过去,通过这种Parent-Child类型关系,我通常让Parent视图模型包含对子视图模型的引用,然后子视图通过事件与父视图通信。

When communicating with other non related viewmodels I've used messaging. 与其他不相关的视图模型进行通信时,我使用了消息传递。

In the project I'm currently working on we're right at the early stages and using mvvm-light and someone has created a similar parent-child relationship using the messaging service, which means we're more loosely coupled, which is great. 在当前正在研究的项目中,我们正处于早期阶段,并且使用mvvm-light,并且有人使用消息传递服务创建了类似的父子关系,这意味着我们之间的联系更加松散,这很棒。 However, we don't have singleton view models, so it's possible that we'll have Parent and child views and viewmodels for Customer A and then other instances again for Customer B. If the child views subscribe to a message - that message is broadcast by Parent A, yet both Child A and B will recieve it, but I only want child A to respond. 但是,我们没有单例视图模型,因此有可能我们为客户A提供父级和子级视图以及Viewmodel,然后为客户B重新提供其他实例。如果子级视图订阅了一条消息,则该消息将被广播由父母A提出,但孩子A和B都会收到,但我只希望孩子A做出回应。

What's the best practice for this kind of scenario? 这种情况下的最佳实践是什么? I was trying to get some timings to compare messaging approach versus the viewmodel references and events approach, but because one approach happens largely via bindings I found it difficult to figure out where to put stopwatch code. 我试图获得一些时间来比较消息传递方法与视图模型引用和事件方法,但是由于一种方法主要是通过绑定发生的,所以我发现很难弄清楚将秒表代码放在哪里。 Does anyone know if there is much performance difference betwen the two approaches. 有人知道这两种方法之间是否存在很大的性能差异。

My current thinking is that we should do it with Parent viewmodel containing references to childviewmodels and utilise the events and bindings for communication. 我目前的想法是,我们应该使用包含对childviewmodel的引用的Parent viewmodel来做到这一点,并利用事件和绑定进行通信。 Or is there a better way of doing this? 还是有更好的方法呢?

I had the same problem and I'm using events to solve it in new views and viewmodels. 我遇到了同样的问题,并且正在使用事件在新的视图和视图模型中解决它。

You can also use Messages to identify the view or viewmodel that should receive it (a kind of id or something like that 8-)). 您还可以使用消息来标识应该接收它的视图或视图模型(一种ID或类似的8-)。

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

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