MVVM:如何处理嵌套ViewModels之间的交互? [英] MVVM: How to handle interaction between nested ViewModels?

查看:1071
本文介绍了MVVM:如何处理嵌套ViewModels之间的交互?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试验所提到的MVVM模式,而且在某些情况下我一直很难界定清晰的边界。在我的应用程序中,我有一个对话框,允许我创建一个与控制器的连接。对话框有一个ViewModel类,它很简单。但是,该对话框还会承载一个额外的控件(由$ code> ContentTemplateSelector 选择),这取决于正在连接的Controller的特定类型。此控件有自己的ViewModel。



我遇到的问题是,当我按OK关闭对话框时,我需要实际创建所请求的连接,需要在内部的Controller特定的ViewModel类中捕获信息。只要简单地让所有的Controller特定的ViewModel类实现一个构建连接的通用接口,但是内部的ViewModel是否真的负责这个构造?



我的一般问题是:有什么普遍接受的设计模式,以使ViewModels如何与彼此交互,特别是当一个父VM需要小孩虚拟机的帮助,以了解该怎么做?






编辑:



我确实想到了一个比一个更干净的设计我最初是在想,但我仍然不知道这是否是正确的方式。我有一些后端服务,允许ContentTemplateSelector查看一个Controller实例,并伪魔法地找到要显示连接构建器的控件。关于这个问题的是,我的顶级ViewModel将必须查看生成的控件的 DataContext ,并将其转换为适当的界面,这似乎是一个坏的想法(为什么View的 DataContext 与创建连接有任何关系?)



我清理了一些东西像这样(简化):

  public interface IController 
{
IControllerConnectionBuilder CreateConnectionBuilder();
}

public interface IControllerConnectionBuilder
{
ControllerConnection BuildConnection();
}

我有我内置的ViewModel类实现 IControllerConnectionBuilder ,Controller返回内部ViewModel。顶级ViewModel然后通过伪魔法机制可视化这个 IControllerConnectionBuilder 。它仍然让我感到有点像是内部的ViewModel执行该建筑,但至少现在我的顶级ViewModel不必知道脏的细节(它甚至不知道或关心可视化的控件使用的是ViewModel)。



如果有办法进一步清理,我还有其他想法。对于ViewModel来说,仍然不清楚它有多少责任。

解决方案

一个非常适用于视图模型之间的交互是直接绑定到坐在viewmodel类之间的观察者


I'm been experimenting with the oft-mentioned MVVM pattern and I've been having a hard time defining clear boundaries in some cases. In my application, I have a dialog that allows me to create a Connection to a Controller. There is a ViewModel class for the dialog, which is simple enough. However, the dialog also hosts an additional control (chosen by a ContentTemplateSelector), which varies depending on the particular type of Controller that's being connected. This control has its own ViewModel.

The issue I'm encountering is that, when I close the dialog by pressing OK, I need to actually create the requested connection, which requires information captured in the inner Controller-specific ViewModel class. It's tempting to simply have all of the Controller-specific ViewModel classes implement a common interface that constructs the connection, but should the inner ViewModel really be in charge of this construction?

My general question is: are there are any generally-accepted design patterns for how ViewModels should interact with eachother, particularly when a 'parent' VM needs help from a 'child' VM in order to know what to do?


EDIT:

I did come up with a design that's a bit cleaner than I was originally thinking, but I'm still not sure if it's the 'right' way to do this. I have some back-end services that allow a ContentTemplateSelector to look at a Controller instance and pseudo-magically find a control to display for the connection builder. What was bugging me about this is that my top-level ViewModel would have to look at the DataContext for the generated control and cast it to an appropriate interface, which seems like a bad idea (why should the View's DataContext have anything to do with creating the connection?)

I wound up with something like this (simplifying):

public interface IController
{
    IControllerConnectionBuilder CreateConnectionBuilder();
}

public interface IControllerConnectionBuilder
{
    ControllerConnection BuildConnection();
}

I have my inner ViewModel class implement IControllerConnectionBuilder and the Controller returns the inner ViewModel. The top-level ViewModel then visualizes this IControllerConnectionBuilder (via the pseudo-magical mechanism). It still bothers me a little that it's my inner ViewModel performing the building, but at least now my top-level ViewModel doesn't have to know about the dirty details (it doesn't even know or care that the visualized control is using a ViewModel).

I welcome additional thoughts if there are ways to clean this up further. It's still not clear to me how much responsibility it's 'okay' for the ViewModel to have.

解决方案

An option which works well for interaction between viewmodels is to bind directly to observer classes sitting between the viewmodel classes.

这篇关于MVVM:如何处理嵌套ViewModels之间的交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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