MVVM中视图模型的交互 [英] Interaction of view models in MVVM

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

问题描述

我有一个遵循MVVM模式的WPF应用程序.到目前为止,该应用程序定义了两个视图和视图模型:

I have a WPF application which follows the MVVM pattern. The application defines two views and view models so far:

  • LoginView(模型)
  • ProjectsView(Model)

两个视图模型都需要访问其他视图模型的多个属性.

Both view models need to access several properties from other view models.

示例:
LoginViewModel具有属性ProjectList. ProjectsViewModel也需要访问此属性.

Example:
LoginViewModel has a property ProjectList. ProjectsViewModel needs to access this property as well.

这只是一个简单的例子.稍后会有几个UserControls,它们都需要相互交互.

This is only a simple example. Later there will be several UserControls which all need to interact with each other.

创建一个将所有UserControls(视图)都设置为其DataContext的巨大视图模型是一个更好的主意吗?如果没有,所有不同的视图模型如何相互交互?

Would it be a better idea to create one huge view model which all UserControls (views) set as their DataContext? If not, how can all the different view models interact with each other?

备注:
这个问题与这个问题密切相关,但又有所不同方法.

Remark:
This question is closely related to this one but with a different approach.

推荐答案

您绝对不应制作庞大的主视图模型"-这是一种反模式,与

You should definitely not make a huge "main view model" -- this is an anti-pattern not dissimilar to a god object.

这里的关键思想是您的视图模型不需要从其他视图模型访问几个属性;相反,所有视图模型都需要访问特定信息.

The key idea here is that your viewmodels do not need to access several properties from other view models; rather, all of the viewmodels need to access specific pieces of information.

现在,您很可能在实例化时将这些信息注入到每个视图模型中.不必这样做,而是为每个视图模型提供对 service 的引用-一个通过属性和/或方法公开此信息的应用程序模块.如果本质上非常简单,则可以标量值的形式公开信息;如果复杂得多,则可以以模型的形式公开信息.

Right now you are most likely injecting this information into each viewmodel at the time of instantiation. Instead of doing this, provide each viewmodel with a reference to a service -- an application module that exposes this information through properties and/or methods. The information can be exposed in the form of scalar values if it's extremely simple in nature, or in the form of models if it's anything more complicated than that.

从原理上讲,这看起来像

Schematically this would look like

/--------------\
|  ViewModelA  |
|              | <=======\
|              |         |
\--------------/         |  <--- information is pulled      /================\
                         +=========[model]===[model]======  |    Service     |
/--------------\         |                                  \================/
|  ViewModelB  |         |
|              | <=======/
|              |
\--------------/

在构建时,应将服务注入到视图模型中(手动或通过DI容器(如果您使用的是一个) ).每个视图模型只需要引用服务,并有足够的信息来告知服务它对哪个模型感兴趣;然后它将从服务中请求该模型,并根据该模型填充其有趣的"属性.

The service should be injected into the viewmodels upon construction (either manually or by the DI container if you are using one). Each viewmodel should only require a reference to the service and enough information to tell to the service which model it's interested in; it will then request that model from the service and populate its "interesting" properties based on that.

这种处理方式比简单地构造一个viewmodel对象并在外部设置其属性要复杂得多,但这将使您的应用程序变得复杂而不会变得难以管理.

This way of doing things is more involved than simply constructing a viewmodel object and setting its properties externally, but it will allow your application to grow in complexity without becoming unmanageable.

例如,如果有很多视图绑定到不同的视图模型,并且这些视图模型以复杂的方式依赖于多个模型,则工作的理智方式将是:

For example, if there are lots of views that bind on different viewmodels and these viewmodels depend in complex ways on a number of models, a sane manner to work would be:

  1. 构建了一个视图/视图模型对
  2. viewmodel从服务请求它需要知道的任何模型;它订阅了服务公开给用户的事件,以使订户知道模型已更改
  3. 通过数据绑定控件对模型进行更改
  4. 视图在视图模型上调用保存"命令
  5. 为此,视图模型在服务上调用了保存此模型"方法
  6. 该服务保留信息并发布模型更改"事件(请参阅第2步)
  7. 对同一模型感兴趣的其他视图模型知道模型已更改,可以查询服务的新状态

如果所有内容都通过服务路由,则有可能.想象一下,要使所有内容保持同步将需要进行什么操作-应对的唯一方法是将所有信息放入一个巨大的视图模型中,并从其他所有内容中引用它们.丑.

This is possible if everything is routed through the service. Imagine what it would take to keep everything synchronized otherwise -- the only way to cope would be to put all the information into a giant viewmodel and reference that from everything else. Ugly.

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

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