MVVM:使用聚合/依赖 ViewModel 设计 ViewModel 架构
WPF/MVVM alpha 极客说:
1 个 View 有 1 个 ViewModel。有时多个视图有 1 个 ViewModel(使用向导)。
如果你看我的图像,你会看到 6 个彩色视图/用户控件。
黄色、绿色和橙色用户控件 在我的应用程序中多次使用。
粉色、蓝色和红色用户控件仅使用一次。
问题:
1.我也应该将它们设为 UserControls 吗?如果是,为什么我不重复使用它们。
2. 假设有 6 个 UserControl,它们是否应该共享相同的 ViewModel? 或者每个视图都应该有自己的 ViewModel 吗?
一个。) 创建绿色类代码 将类别代码发送至 YELLOW
B。) 选择黄色的类别代码 更改蓝色中的当前瞳孔
C.) 选择蓝色中的当前学生 更改红色中的学生详细信息 中的学生文档
更改橙色D 。) 创建粉红色的瞳孔 将学生发送到 BLUE
E。)...更多
这是正确的方法吗,使用 Messenger 类来发送数据以保持关系最新?
对我来说有一个主要缺陷:
我创建了一个 PupilViewModel,但我不知道 NewPupilViewModel(PINK) 中黄色用户控件中是否存在 SchoolclassCodeViewModel,因此我可以将新的 PupilViewModel 添加到蓝色用户控件中。
SchoolclassCodeViewModel 1 : N PupilViewModel。
3.你会如何解决这个问题?
The WPF/MVVM alpha geeks say:
1 View has 1 ViewModel. Sometimes Multiple Views has 1 ViewModel (using a Wizard).
If you regard my image you see 6 colored Views/UserControls.
The YELLOW,GREEN and ORANGE UserControls
are used multiple times in my application.
The pink,blue and red UserControls are used only once.
Questions:
1. Should I make them UserControls too? If yes, why if I do not reuse them.
2. Assume those are 6 UserControls, should they share the same ViewModel?
OR should each View have its own ViewModel?
A.)
Create Classcode in GREEN
Send class code to YELLOW
B.)
Select class code in YELLOW
Change current pupil in BLUE
C.)
Select current pupil in BLUE
Change pupil details in RED
Change pupil documents in ORANGE
D.)
Create pupil in PINK
Send pupil to BLUE
E.)... many more
Is that the way to go, firing around data with a Messenger class to keep the relations up to date?
There is a major flaw for me:
I create a PupilViewModel but I do not know in the NewPupilViewModel(PINK) wether a SchoolclassCodeViewModel exists in the YELLOW UserControl so I could add my new PupilViewModel to the BLUE UserControl.
SchoolclassCodeViewModel 1 : N PupilViewModel.
3. How would you solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
丹的方法与我要建议的方法几乎相同。要回答具体问题:
1)如果它们的布局有任何复杂性,我会让它们成为用户控件。其一,一致地做每件事会更容易;另一方面,这使得在 Expression Blend 中使用该控件的布局变得更加容易,而无需实例化整个窗口。
2)整个窗口有应用程序视图模型。它包含需要它们的视图的属性:
2A)
ClassCodeViewModel
公开一个CreateCommand
,该命令在执行时引发事件。窗口视图模型处理此事件,并在引发该事件时对 ClassCodes 进行适当的更新。2B) 类代码视图模型包含瞳孔视图模型的可观察集合。类代码视图模型上的
SelectedItem
绑定到的窗口视图模型中SelectedClassCode
属性中的SelectedItem
属性。蓝色视图绑定到SelectedClassCode.Pupils
。2C) 类似地,窗口视图模型包含一个
SelectedPupil
属性,蓝色视图的SelectedItem
属性绑定到该属性。红色视图绑定到SelectedPupil
。2D) 其处理方式与 2A 中的处理方式相同:瞳孔视图模型引发事件,窗口视图模型处理该事件。
3) 瞳孔视图模型包含一个布尔
CanCreate
属性。这在红色视图中不使用。窗口视图模型在SelectedClassCode
的setter 中设置NewPupil.CanCreate
。您没有询问,但橙色视图绑定到
SelectedPupil
的Documents
属性,这可能是(取决于实际文档是什么)的可观察集合>DocumentViewModel
对象。也许我已经在 MVVM 领域耕耘太久了,但这对我来说似乎非常简单。
Dan's approach is pretty much identical to what I was going to suggest. To answer the specific questions:
1) I would make them UserControls if there's any complexity to their layout at all. For one, it's easier to do everything consistently; for another, this makes it easier to play around with the layout of that control in Expression Blend without having to instantiate an entire window.
2) There's application view model for the entire window. It contains properties for the views that need them:
2A)
ClassCodeViewModel
exposes aCreateCommand
that raises an event when it's executed. The window view model handles this event and does the appropriate update toClassCodes
when it's raised.2B) The class code view model contains an observable collection of pupil view models. The
SelectedItem
property in theSelectedClassCode
property in the window view model that theSelectedItem
on the class codes view model is bound to. The blue view is bound toSelectedClassCode.Pupils
.2C) Similarly, the window view model contains a
SelectedPupil
property that theSelectedItem
property of the blue view is bound to. The red view is bound toSelectedPupil
.2D) This is handled the same way that it's handled in 2A: the pupil view model raises an event and the window view model handles it.
3) The pupil view model contains a boolean
CanCreate
property. This isn't used in the red view. The window view model setsNewPupil.CanCreate
in the setter ofSelectedClassCode
.You didn't ask, but the orange view is bound to the
Documents
property ofSelectedPupil
, which is probably (depending on what documents actually are) an observable collection ofDocumentViewModel
objects.Maybe I've been toiling in the fields of MVVM for too long, but this seems extremely straightforward to me.