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

问题描述

为了掌握 MVVM 的概念,我已经阅读了几篇博客并查看了一些项目.

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

据我所知,View 是愚蠢的,它只知道如何呈现传递给它的东西.

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

Models 只是普通数据,而 ViewModel 就像是两者之间的填充物,它应该从 Model 获取信息 并将其传递给 ViewView 应该知道如何呈现它.或者反过来,如果 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 应用程序建模的对象,这意味着它们提供视图使用的数据和功能.它们定义了您正在构建的实际应用程序的结构和行为.对于模型对象,域是您选择的任何域(音乐商店、组织结构图浏览器等),但对于视图模型,域是图形应用程序.您的视图模型将封装您的应用程序所做的一切的行为和数据.他们将对象和列表公开为属性,以及诸如命令之类的东西.命令只是一种行为(最简单的是方法调用),它被包裹在一个携带它的对象中——这个想法很重要,因为视图是由数据绑定驱动的,数据绑定将可视化控件附加到对象上.在 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:

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

兔子洞更深了 - 有很多习惯用法需要弄清楚,比如保持 MVVM 工作的 ValueConverters,当您开始考虑诸如 Blendability、测试以及如何在您的应用程序中传递数据之类的事情时,有很多东西可以应用并确保每个视图模型都可以访问它需要的行为(这是依赖注入的用武之地),但希望以上是一个好的开始.关键是将您的视觉效果、领域以及实际应用程序的结构和行为视为三件不同的事情.

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 应该做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:13