Views、Models 和 ViewModels 之间的共同关系是什么? [英] What are the common relationships between Views, Models, and ViewModels?

查看:68
本文介绍了Views、Models 和 ViewModels 之间的共同关系是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与设计人员一起开发 Windows Phone 7 应用程序.我使用 XNA 进行过 C# 开发,所以我了解 C#,但不熟悉 Model/View/ViewModel 架构.

I'm working on a Windows Phone 7 app with a designer. I've done C# development with XNA so I know C# but was not familiar with the Model/View/ViewModel architecture.

我们对它的第一次破解对任何给定的视图都有多个处于活动状态的 ViewModel.每个 ViewModel 都是我们模型中每个对象的中间层.

Our first crack at it had multiple ViewModels active for any given view. Each ViewModel was an in-between layer for each object in our model.

例如:我们有一个朋友"页面,它有一个显示朋友列表的 ListBox.因此,我们创建了一个 FriendsListViewModel 来处理从 XAML 将绑定到的模型中获取 ObservableCollection 的操作.页面中还有其他可用功能(导航到其他页面、激活半相关功能等),因此包含在 FriendsPageViewModel 中.

For example: We had a "Friends" page that has a ListBox that displays the list of Friends. So we made a FriendsListViewModel that would handle getting an ObservableCollection<Friend> from the Model that the XAML would bind to. There were other functions available in the page (navigating to other pages, activating semi-related features, etc.) so that was contained in the FriendsPageViewModel.

我开始觉得这很疯狂,所以我将 View 和 ViewModel 之间的关系设为 1:1.

This was starting to look crazy to me, so I made the relationship between View and ViewModel 1:1.

综上所述,我有一个包含两个组成部分的问题:有了 MVVM,Views 和 ViewModels 之间的共同关系是什么?(此处重点介绍 Windows Phone 7 开发,以防它与 ASP.NET、WPF 或 Silverlight 有任何不同)

With all that described, I've got a question with two components: With MVVM, what is the common relationship between Views and ViewModels? (focusing on Windows Phone 7 development here in case it's any different from ASP.NET, WPF, or Silverlight)

作为该问题的一个可能的补充:假设 1:1 关系是普遍正确的或被接受的关系:如果您正在处理诸如 Pivot 控件或 Panorama 控件,您通常会给每个 PivotItemPanoramaItem 自己的 ViewModel 吗?

And as a possible addon to that question: Say the 1:1 relationship is the generally correct or accepted one: If you're dealing with something like a Pivot control or Panorama control, would you typically give each PivotItem or PanoramaItem its own ViewModel?

推荐答案

在 MVVM 中,通常每个视图都有一个 ViewModel(存在例外情况).View 通常绑定"到 ViewModel,它是视图和数据模型之间的粘合剂.您的视图可以包含多个控件,每个控件将绑定到您的 ViewModel 上的特定属性(即数据源).一旦更新了这些属性之一(通过 C# 中的 INotifyPropertyChanged 接口),ViewModel 将通知 View.

In MVVM, you typically have one ViewModel for each View (exceptions exist). The View typically 'binds' to the ViewModel which is the glue between the view and the data model. Your view can contain multiple controls and each control will bind to a particular property (i.e. data source) on your ViewModel. The ViewModel will then notify the View once one of these properties is updated (via the INotifyPropertyChanged interface in C#).

在考虑 ViewModel 时,不要将其视为每个控件的单个 ViewModel.想想控件绑定到共享 ViewModel 的单个属性.

When thinking about ViewModels, don't think of it as a single ViewModel per control. Think of the control binding to a single property of the shared ViewModel.

ViewModel 将数据从模型提供给视图.View 应该只用于显示它从 ViewModel 获取的数据.将视图中的代码保持在最低限度,并且仅与渲染控制元素有关.ViewModel 负责从任何数据源查询数据,然后提供 View 可以挂钩的公共属性.

The ViewModel provides data from the model to the View. The View should only be used to display the data it gets from the ViewModel. Keep code in the View to a mimimum and only pertaining to rendering control elements. The ViewModel is responsible for querying data from whatever the data source may be and then providing public properties the View can hook into.

这个 MSDN 链接 有一篇非常详细的文章,但是您可以在 wikipedia 上获得很好的概要.

This MSDN link has a pretty detailed article on it, but you can get a good synopsis on wikipedia.

这篇关于Views、Models 和 ViewModels 之间的共同关系是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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