简体   繁体   English

视图、模型和视图模型之间的常见关系是什么?

[英]What are the common relationships between Views, Models, and ViewModels?

I'm working on a Windows Phone 7 app with a designer.我正在与设计师一起开发 Windows Phone 7 应用程序。 I've done C# development with XNA so I know C# but was not familiar with the Model/View/ViewModel architecture.我已经用 XNA 完成了 C# 开发,所以我知道 C# 但不熟悉模型/视图/视图模型架构。

Our first crack at it had multiple ViewModels active for any given view.我们对它的第一次破解为任何给定视图提供了多个激活的 ViewModel。 Each ViewModel was an in-between layer for each object in our model.每个 ViewModel 都是 model 中每个 object 的中间层。

For example: We had a "Friends" page that has a ListBox that displays the list of Friends.例如:我们有一个“朋友”页面,其中有一个显示朋友列表的列表ListBox So we made a FriendsListViewModel that would handle getting an ObservableCollection<Friend> from the Model that the XAML would bind to.所以我们创建了一个FriendsListViewModel来处理从 XAML 将绑定到的 Model 获取ObservableCollection<Friend> There were other functions available in the page (navigating to other pages, activating semi-related features, etc.) so that was contained in the FriendsPageViewModel.页面中还有其他可用功能(导航到其他页面、激活半相关功能等),因此包含在 FriendsPageViewModel 中。

This was starting to look crazy to me, so I made the relationship between View and ViewModel 1:1.这对我来说开始看起来很疯狂,所以我将 View 和 ViewModel 之间的关系设置为 1:1。

With all that described, I've got a question with two components: With MVVM, what is the common relationship between Views and ViewModels?综上所述,我有两个组件的问题:对于 MVVM,视图和视图模型之间的共同关系是什么? (focusing on Windows Phone 7 development here in case it's any different from ASP.NET, WPF, or Silverlight) (重点关注 Windows Phone 7 开发,以防它与 ASP.NET、WPF 或 Silverlight 有任何不同)

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?作为该问题的一个可能的补充:假设 1:1 关系是普遍正确或可接受的关系:如果您正在处理Pivot控件或Panorama控件之类的东西,您通常会给每个PivotItemPanoramaItem自己的 ViewModel 吗?

In MVVM, you typically have one ViewModel for each View (exceptions exist).在 MVVM 中,通常每个 View 都有一个 ViewModel(存在例外)。 The View typically 'binds' to the ViewModel which is the glue between the view and the data model. View 通常“绑定”到 ViewModel,ViewModel 是 View 和数据 model 之间的粘合剂。 Your view can contain multiple controls and each control will bind to a particular property (ie data source) on your ViewModel.您的视图可以包含多个控件,每个控件将绑定到您的 ViewModel 上的特定属性(即数据源)。 The ViewModel will then notify the View once one of these properties is updated (via the INotifyPropertyChanged interface in C#).一旦这些属性之一被更新(通过 C# 中的 INotifyPropertyChanged 接口),ViewModel 将通知 View。

When thinking about ViewModels, don't think of it as a single ViewModel per control.在考虑 ViewModel 时,不要将其视为每个控件的单个 ViewModel。 Think of the control binding to a single property of the shared ViewModel.考虑将控件绑定到共享 ViewModel 的单个属性。

The ViewModel provides data from the model to the View. ViewModel 将来自 model 的数据提供给 View。 The View should only be used to display the data it gets from the ViewModel. View 应该只用于显示它从 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. ViewModel 负责从任何数据源查询数据,然后提供 View 可以挂钩的公共属性。

This MSDN link has a pretty detailed article on it, but you can get a good synopsis on wikipedia .这个MSDN 链接上有一篇非常详细的文章,但您可以在wikipedia上获得一个很好的概要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM