简体   繁体   English

视图应该只绑定到 mvvm 中的视图模型吗?

[英]Should a View only bind to a ViewModel in mvvm?

Recently, I have to deal with mvvm pattern in my new xamarin forms project.最近,我不得不在我的新 xamarin 表单项目中处理 mvvm 模式。 The Page code on XAML which have bindingContext is ViewModel. XAML 上具有 bindingContext 的页面代码是 ViewModel。 The hard part is my Page need to use more than one ViewModel to accomplish my task.困难的部分是我的 Page 需要使用多个 ViewModel 来完成我的任务。 eg My HomePage need to use data in contact ViewModel, user ViewModel, city ViewModel, ...例如,我的主页需要在联系人视图模型、用户视图模型、城市视图模型、...

I've searched on the internet and people say that we should use only a viewmodel for a specified view.我在互联网上搜索过,人们说我们应该只对指定视图使用视图模型。 So, I wonder if i should create a new viewmodel which wrap all of above viewmodel OR i should set different bindingContext for each of child view in that page.所以,我想知道我是否应该创建一个新的视图模型来包装上述所有视图模型,或者我应该为该页面中的每个子视图设置不同的 bindingContext 。

Anyone experienced in this case and which one is the best solution.任何在这种情况下有经验的人,哪一个是最好的解决方案。

I would have created HomePageViewModel which encapsulates some other classes. 我会创建HomePageViewModel,它封装了一些其他类。

class HomePageViewModel
{
    UserViewModel user;
    ...
}

HomePageViewModel will become DataContext of HomePage and the child layouts will be assigned respective properties. HomePageViewModel将成为HomePage的DataContext,子布局将分配相应的属性。

The idea that a 1:1 relationship is somehow antithetical to a view model hierarchy is misguided, to say the least. 至少可以说,1:1关系在某种程度上与视图模型层次结构相对立的想法是错误的。 The view model is a logical representation of the view; 视图模型是视图的逻辑表示; implementing a hierarchy of view models thus not only adheres to the architecture it's practically a requirement for anything more complicated than a "Hello World" app! 实现视图模型的层次结构因此不仅遵循体系结构,它实际上是比“Hello World”应用程序更复杂的要求! Any real-world application will have a complex visual hierarchy of views, so by definition the 1:1 relationship mandates a similar hierarchy within the view model. 任何真实世界的应用程序都将具有复杂的视图层次结构,因此根据定义,1:1关系要求视图模型中的类似层次结构。 This is just common sense. 这只是常识。

But don't just take my word for it, take a look at any code written by the experts on this topic and you'll see they all do the same thing. 但是,不要只听我的话,看看专家就这个主题写的任何代码,你会看到他们都做同样的事情。 A good starting point is Chapter 4 of Josh Smith's "Advanced MVVM" (ViewModel Architecture Overview) in which even his simple application contains a high-level VM encapsulating both the game and game-over VMs, and with the game VM itself consisting of separate VMs for the field and individual game elements. 一个很好的起点是Josh Smith的“高级MVVM”(ViewModel体系结构概述)的第4章,其中即使是他的简单应用程序也包含一个高级VM,它封装了游戏和游戏虚拟机,并且游戏VM本身也是独立的用于场和各个游戏元素的VM。

I wonder if i should create a new viewmodel which wrap all of above viewmodel OR i should set different bindingContext for each of child view in that page. 我想知道我是否应该创建一个新的viewmodel来包装所有上面的viewmodel 或者我应该为该页面中的每个子视图设置不同的bindingContext。

You should do both. 你应该两个都做。

The ViewModel serves the View, a View should only bind to one VM. ViewModel为View提供服务,View只应绑定到一个VM。

My HomePage need to use data in contact ViewModel, user ViewModel, city ViewModel, ... 我的HomePage需要使用联系ViewModel,用户ViewModel,城市ViewModel,...

When your Homepage displays that data in Child Views (UserControl, DataTemplate) then the ViewModel should have properties holding those other ViewModels. 当您的主页在子视图(UserControl,DataTemplate)中显示该数据时,ViewModel应具有保存其他ViewModel的属性。 Possibly in ObservableCollections. 可能在ObservableCollections中。

A contact ViewModel should exist because there is a Contact (child) View, not because there is a Contact Model class. 应该存在联系人ViewModel,因为有一个Contact(子)视图,而不是因为有一个Contact Model类。

Yes, a view should only bind to a single View Model in MVVM. 是的,视图应该只绑定到MVVM中的单个视图模型。 I certainly wouldn't derive from or wrap other view models. 我当然不会派生或包装其他视图模型。 The main idea here is to use the models that are required by your view in a view model that is specific to your view so you can see easily see exactly what this view requires. 这里的主要思想是在视图特定于视图的模型中使用视图所需的模型,以便您可以轻松查看此视图​​所需的内容。 It also allows you to simplify your code as you will only have the code needed to make the view work, nothing more, nothing less. 它还允许您简化代码,因为您只需要使视图工作所需的代码,仅此而已。

With WPF, using the view model as the parent binding context works well even if your models are fairly complicated because it allows for class navigation. 使用WPF,即使您的模型相当复杂,使用视图模型作为父绑定上下文也能很好地工作,因为它允许类导航。 For instance, you can bind to DataContext.User.Profile.FirstName in your view model. 例如,您可以绑定到视图模型中的DataContext.User.Profile.FirstName So, you could have a view model that has a property for user, contact, and address. 因此,您可以拥有一个具有用户,联系人和地址属性的视图模型。

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

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