MVVM - 什么是用户控件来相互交谈的理想方式 [英] MVVM - what is the ideal way for usercontrols to talk to each other
问题描述
我有一个包含多个其他用户控件的用户控件。我使用MVVM。每个用户的控制都有相应的虚拟机。如何将这些用户控制信息发送给对方?我想避免在XAML code编写任何code后面。特别是我感兴趣的控制(主要用户控件中)将如何互相沟通,以及如何将他们谈论到容器用户控件。
I have a a user control which contains several other user controls. I am using MVVM. Each user control has a corresponding VM. How do these user controls send information to each other? I want to avoid writing any code in the xaml code behind. Particularly I am interested in how the controls (inside the main user control) will talk to each other and how will they talk to the container user control.
修改:
我知道,使用事件,代表们帮我解决这个问题。但是,我想避免在XAML $ C $编写任何code C-落后。
EDIT: I know that using events-delegates will help me solve this issue. But, I want to avoid writing any code in xaml code-behind.
推荐答案
通常情况下,最好尽量减少零件之间的通信量,因为每次两个用户控件交谈对方,你引入它们之间的依赖关系。
Typically, it's best to try to reduce the amount of communication between parts, as each time two user controls "talk" to each other, you're introducing a dependency between them.
话虽这么说,有几件事情要考虑:
That being said, there are a couple of things to consider:
- 用户控件始终可以通过公开的属性和使用数据绑定对话包含他们的控制。这是非常好的,因为它preserves的MVVM风格的各个方面。
- 含控制可以使用属性为链接两个用户的两个属性控制起来,再次,preserving干净的边界
如果您确实需要有更明确的沟通,主要有两种技术途径。
If you do need to have more explicit communication, there are two main approachs.
- 实施常见的两种元素的服务,并使用依赖注入,以提供运行时的执行。这让控制通话的服务,其可以反过来,保持控制同步,但也保持依赖性最小。
- 使用某种形式的消息传递到控件之间传递消息。许多MVVM框架采取这种方法,因为它能够消除接收消息,再次,保持相关性降至最低发送消息。
这篇关于MVVM - 什么是用户控件来相互交谈的理想方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!