简体   繁体   English

WPF MVVM在视图之间进行通信

[英]WPF MVVM communicate between views

Lets assume I have a button in View A which should trigger functionality of View B. How is this preferably done? 假设我在视图A中有一个按钮,该按钮应该触发视图B的功能。最好如何做到这一点? I could use the Mediator but I prefer to avoid it and mainly use it for communication between viewmodels where there is absolutely no other way. 我可以使用调解器,但我倾向于避免使用调解器,而主要将其用于视图模型之间的通信,而绝对没有其他方法。

Here is a thought of mine: 这是我的想法:

在此处输入图片说明

So basically we have an interceptor class that handles communication between multiple views. 因此,基本上,我们有一个拦截器类来处理多个视图之间的通信。 The view doesnt know about the other views. 该视图不了解其他视图。 The interceptor acts as a messenger between them. 拦截器充当它们之间的使者。

Is this ok in terms of thinking in MVVM? 在MVVM中思考是否可以?

Most MVVM frameworks contains a mediator of some kind for this purpose. 为此,大多数MVVM框架都包含某种介体。

In MvvmLight it is called Messenger and in Prism it is called EventAggregator . MvvmLight它称为Messenger ;在Prism它称为EventAggregator For example. 例如。

And the mediator can be used to communicate between any two components, be it a view or a view model, in a loosely coupled way. 中介可以以松散耦合的方式用于在任何两个组件(视图或视图模型)之间进行通信。

So you are basically describing the mediator pattern here. 因此,您基本上是在这里描述中介模式。 Don't reinvent the wheel :) 不要重新发明轮子:)

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

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