MVVM的基本概念-ViewModel应该做什么? [英] Basic concepts of MVVM-- what should a ViewModel do?

查看:104
本文介绍了MVVM的基本概念-ViewModel应该做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试掌握MVVM的概念,我已经阅读了多个博客,并研究了一些项目.

Trying to grasp the concepts of MVVM, I have already read several blogs and looked at a few projects.

据我了解,视图是愚蠢的,它只知道如何呈现传递给它的内容.

From what I understand, a View is dumb, it just knows how to present something that is passed to it.

模型只是纯数据,而 ViewModel 的作用类似于两者之间的填充,它应从 Model中获取信息并将其传递到 View 上, View 应该知道如何显示它.或相反,如果 View 中的信息发生更改,则应将更改传递给 Model .

Models are just the plain data, and a ViewModel is something that acts like a padding in between the two, that it should get information from the Model and pass it onto the View, and the View should know how to present it. Or the other way around, if the information in the View changes, it should pass on the change to the Model.

但是我仍然不知道如何应用这个概念.有人可以解释一个非常简单的情况,以便我理解这个概念吗?我已经看过几个项目,但是仍然没有完全意义,因此,如果有人可以用简单的英语写出来,那就太好了.

But I still have no idea how to apply the concept. Can someone explain a very simple scenario so I can grasp the concept? I've already looked at several projects, but it still doesn't make complete sense, so if someone could write it out in plain English, that would be nice.

推荐答案

我喜欢这样想:

您所说的观点很愚蠢.乔希·史密斯(Josh Smith)是开创性著作的作者,经常在MVVM上链接 MSDN文章,他说观点是数据穿的衣服."视图从不实际包含或直接处理数据,它们仅绑定到视图模型的属性和命令.

Views, as you say, are dumb. Josh Smith, writer of the seminal and often linked MSDN article on MVVM, has said that views are "the clothes that data wears." Views never actually contain data or directly manipulate it, they are just bound to properties and commands of your viewmodels.

模型是在业务对象中对应用程序域进行建模的对象.您的应用程序是音乐商店吗?也许您的模型对象将是艺术家,专辑和歌曲.您的应用程序是组织结构图浏览器吗?也许您的模型对象将是经理和员工.这些模型对象与任何形式的视觉渲染都不相关,它们甚至与您要放入其中的应用程序都不直接相关-模型对象作为代表某种类型的对象家族应该完全有意义.域.模型层通常还包括诸如服务访问器之类的东西.

Models are objects that model the domain of your application, as in business objects. Is your application a music store? Perhaps your model objects will be artists, albums and songs. Is your application an org-chart browser? Perhaps your model objects will be managers and employees. These model objects are not related to any kind of visual rendering, and they aren't even directly related to the application you're putting them into - your model objects should make sense completely on their own as a family of objects that represent some kind of domain. The model layer also typically includes things like service accessors.

这使我们进入了Viewmodels.这些是什么?它们是为 GUI应用程序建模的对象,这意味着它们提供了供视图使用的数据和功能.它们定义了正在构建的实际应用程序的结构和行为.对于模型对象,该域是您选择的任何域(音乐商店,组织结构图浏览器等),但是对于viewmodel,该域是图形应用程序.您的视图模型将封装应用程序所做的所有行为和数据.他们将对象和列表作为属性公开,以及诸如Commands之类的东西.命令只是包裹在携带该对象的对象中的一种行为(最简单的方法调用)-这个想法很重要,因为视图是由数据绑定驱动的,该绑定将可视化控件附加到对象上.在MVVM中,您无需为按钮提供Click处理程序方法,而是将其绑定到命令对象(从视图模型中的属性提供),该命令对象包含您要在单击时运行的功能.

This brings us to Viewmodels. What are they? They are objects that model a GUI application, meaning they provide data and functionality to be used by views. They are what define the structure and behavior of the actual application you are building. For the model objects, the domain is whatever domain you choose (music store, org-chart browser, etc.), but for the viewmodel, the domain is a graphical application. Your viewmodels are going to encapsulate the behavior and the data of everything your application does. They are going to expose objects and lists as properties, as well as things like Commands. A command is just a behavior (at its simplest, a method call) wrapped up into an object that carries it around - this idea is important because views are driven by databinding, which attaches visual controls to objects. In MVVM, you don't give a button a Click handler method, you bind it to a command object (served up from a property in a viewmodel) that contains the functionality you want to run when you click it.

对我来说,最令人困惑的地方如下:

For me, the most confusing bits were the following:

  • 即使视图模型是图形应用程序的模型,它们也不直接引用或使用视觉概念.例如,您不希望在ViewModels中引用Windows控件-这些东西会出现在视图中. ViewModel只是向控件或将绑定到它们的其他对象公开数据和行为.例如-您是否有一个带有ListBox的视图?您的视图模型几乎肯定会包含其中的某种集合.您的视图有按钮吗?您的视图模型几乎肯定会包含一些命令.
  • 有几种对象可以视为视图模型".最简单的一种视图模型是直接以1:1关系表示控件或屏幕的视图模型,例如屏幕XYZ具有一个文本框,一个列表框和三个按钮,因此,视图模型需要一个字符串,一个集合,和三个命令."适合视图模型层的另一种对象是模型对象的包装,该模型对象赋予其行为并使其在视图中更有用-在这里,您可以了解厚"和薄"视图模型层的概念. 薄"视图模型层是一组视图模型,这些视图模型将模型对象直接暴露给视图,这意味着视图最终直接绑定到模型对象的属性.这可以用于简单的只读视图之类的事情,但是如果您希望每个对象都具有相关的行为怎么办?您不希望在模型中使用该模型,因为该模型与应用程序无关,而仅与您的域相关.您可以将其放在包装模型对象的对象中,并提供更多对绑定友好的数据和行为.这个包装对象也被认为是一个视图模型,让它们产生一个较厚"的视图模型层,在该层中,视图永远不会最终直接绑定到模型类上的任何内容.集合将包含包装模型的视图模型,而不仅仅是包含模型本身.

难题越来越深-有很多习惯用法可以使ValueVM保持正常运行,例如ValueConverters,当您开始考虑诸如可混合性,测试以及如何在应用程序中传递数据之类的东西时,会有很多应用并确保每个视图模型都可以访问所需的行为(这是依赖注入的来源),但希望以上内容是一个好的开始.关键是将您的视觉效果,您的领域以及实际应用程序的结构和行为视为三件不同的事情.

The rabbit hole goes deeper - there are lots of idioms to figure out like ValueConverters that keep MVVM working, and there's a lot to apply when you start thinking about things like Blendability, testing, and how to pass data around in your app and ensure that each viewmodel has access to the behavior it needs (this is where dependency injection comes in), but hopefully the above is a good start. The key is to think about your visuals, your domain, and the structure and behavior of your actual application as three different things.

这篇关于MVVM的基本概念-ViewModel应该做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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