简体   繁体   English

视图模型/实体的多个视图还是一个视图中的多个视图模型?

[英]Multiple views of viewmodel/entities or multiple viewmodels in one view?

Still trying to find good example of the more complex MVVM scenarios... 仍在尝试寻找更复杂的MVVM场景的好例子...

Suppose I have one viewmodel - PlayersViewModel that has a collection of Players. 假设我有一个viewmodel-PlayersViewModel,它具有一组Players。 In one view I see the collection of Players and add/edit/delete. 在一个视图中,我看到播放器的集合并添加/编辑/删除。

Another view is teams, where I assign Players to Teams. 另一个视图是团队,在这里我将玩家分配给团队。 So I have another viewmodel - TeamsViewModel. 所以我有另一个viewmodel-TeamsViewModel。 This view also needs a collection of Players. 此视图还需要一个Player集合。 How do I keep the two player lists in sync so changes are shown in both views? 如何保持两个玩家列表同步,以便在两个视图中都显示更改?

I see a number of options: 我看到许多选择:

  1. Add a reference to PlayersViewModel to my Team view (as well as a reference to TeamsViewModel) and use the PlayersViewModel.Players collection in both views 将对PlayersViewModel的引用添加到我的团队视图(以及对TeamsViewModel的引用),并在两个视图中使用PlayersViewModel.Players集合
  2. Have two different collections that reference the same underlying collection instance (somehow) 有两个引用同一个基础集合实例的不同集合(以某种方式)
  3. Create a static property on the Player model like Player.All that returns the collection and the viewmodels manage Players by Player.Add(player), Player.Delete, etc. instead of PlayersViewModel.AddPlayer(player)? 在Player模型上创建一个静态属性,例如Player.All,所有返回集合,并且视图模型通过Player.Add(player),Player.Delete等(而不是PlayersViewModel.AddPlayer(player))管理Players?

I tend towards #1 at the moment and using app-wide resources so the Team view can call both viewmodels. 目前,我倾向于使用应用程序范围的资源来排名第一,因此Team视图可以同时调用两个视图模型。 But then how do I use the selected Players in my PlayersViewModel.Players collection in my TeamsViewModel to add them? 但是,如何在TeamsViewModel的PlayersViewModel.Players集合中使用选定的Player来添加他们呢?

Help please!! 请帮助!!

My hard and fast rule is one ViewModel per View, so in my book you are on the right path. 我的硬性规定是每个视图一个ViewModel,因此在我的书中,您走的路是正确的。 Don't confuse the PlayersViewModel with the Players data: the PlayersViewModel is oriented toward the Players View, NOT the Players data. 不要将PlayersViewModel与Players数据混淆:PlayersViewModel面向Players视图,而不是Players数据。 In other words, the two are separate, so you do not need to reuse the PlayersViewModel in other ViewModels. 换句话说,两者是分开的,因此您无需在其他ViewModels中重用PlayersViewModel。 I apologize if I'm not explaining this well... 如果我不能很好地解释这一点,我深表歉意。

If you need multiple ViewModels to display with the same instance of the data, then define the data at the App level rather than the Document level. 如果您需要多个ViewModel来显示相同​​的数据实例,请在App级别而不是Document级别定义数据。 You could make Players Static or you could have it implement the Singleton pattern: none of those things are specific to the ViewModel because the ViewModel merely consumes the resource. 您可以将Players设为静态,也可以使其实现Singleton模式:这些都不是特定于ViewModel的,因为ViewModel仅消耗资源。

Use a single ViewModel. 使用单个ViewModel。 Let different views display only what they need. 让不同的视图仅显示它们所需的内容。 With regard to the collection of players: WPF allows you to have multiple views of the same collection and each with a different filtering/sorting/grouping. 关于播放器的集合:WPF允许您拥有同一集合的多个视图,每个视图具有不同的过滤/排序/分组。 See Binding to Collections . 请参阅绑定到集合

Personnally, in order to be easier to understand, I have one viewmodel per view. 就个人而言,为了更容易理解,每个视图都有一个视图模型。 This means each custom UserControl has its own ViewModel, dealing with its own actions. 这意味着每个自定义UserControl都有自己的ViewModel,用于处理自己的动作。 I'm working on a pretty big project with plenty of views, I think it is way cleaner to have one ViewModel per view. 我正在一个具有大量视图的大型项目中工作,我认为每个视图有一个ViewModel会更干净。 It helps me reading my architecture correctly, and therefore I won't mix roles in a unique ViewModel. 它可以帮助我正确阅读架构,因此不会在唯一的ViewModel中混合角色。

However, I cannot ensure you that's the best way to do. 但是,我不能保证这是最好的方法。 I started working in WPF/MVVM 2 weeks ago, I just figured out that it'd be easier to understand with this way (I am used to split my programs into as many classes as possible since I think it's easier to maintain) 我两周前开始在WPF / MVVM中工作,我发现用这种方法更容易理解(因为我认为维护起来更容易,所以我习惯将我的程序分成尽可能多的类)

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

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