MVVM:查找其他ViewModel [英] MVVM: locating other ViewModels

查看:111
本文介绍了MVVM:查找其他ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MVVM应用程序中有很多父详细视图模型.像这样:

I have quite a large number of parent-detail ViewModels in my MVVM application. Something like this:

SchoolsViewModel
  +- SchoolViewModel
      +- LessonViewModel
          +- PupilsViewModel
              +- PupilViewModel
          +- TeacherViewModel
      +- PupilsViewModel
          +- PupilViewModel
              +- LessonsViewModel
      +- TeachersViewModel

依此类推...

此外,一个单一的视图模型可以出现在多个地方,具体取决于用户是按课程还是按学生浏览等等.

In addition, a single view model can appear in more than one place, depending on whether the user is browsing by lesson or pupil, etc.

您如何允许在不同的父ViewModel之间共享子ViewModel?例如,"Pupil A"将出现在最高级别的PupilsViewModel中,并且还将出现在LessonViewModels中包含的许多PupilsViewModel中.您是否将创建多个引用相同数据模型的PupilViewModel对象?还是以某种方式找到数据模型的现有视图模型?

How would you allow for sharing of child ViewModels between different parent ViewModels? For example, "Pupil A" will be present in the highest-level PupilsViewModel and also in a number of PupilsViewModels contained within LessonViewModels. Would you create multiple PupilViewModel objects referring to the same data model? Or somehow locate an existing view model for the data model?

该问题还有另一个相关问题: MVVM和StructureMap的使用

This question has another related question: MVVM and StructureMap usage

推荐答案

我建议只拥有一个学生A实例.这样,当用户在一个地方更新瞳孔时,该瞳孔在应用程序中的其他所有位置都会更新. .为此,您需要在每个ViewModel上实现INotifyPropertyChanged,但这是MVVM中的标准做法.

I would suggest having only one instance of Pupil A. That way, when the user updates a pupil in one place, that pupil is updated everywhere else in the application. In order to accomplish this, you need to implement INotifyPropertyChanged on each ViewModel, but this is standard practice in MVVM.

在您的情况下,建议您使用CollectionViews将PupilsViewModel(集合)的不同视图提供给应用程序的不同部分.这样,它们就可以对相同的基础数据进行操作,但是应用程序的不同部分可以独立地浏览它们.

In your case, I suggest using CollectionViews to provide different views of your PupilsViewModel (collection) to different parts of your application. That way they're operating on the same underlying data, but the different parts of the application can navigate over them independently.

这篇关于MVVM:查找其他ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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