ViewModel 作为 ViewModel 的成员
我一直在使用 MVVM Light Toolkit 来帮助学习 MVVM 模式。但是,我无法解决控件场景中的用户控件问题。
例如,在时间表应用程序中,假设我们有一个名为 NewUnitOfWork 的控件。首次加载时,带有包含项目列表的 ListBox 的面板将作为 NewUnitOfWork 的内容加载。用户单击其中一个。新面板与包含该项目可能任务的列表框交换。选择一个任务并加载一个新面板,其中包含用于为所选项目的所选任务输入数据的控件。
因此,我们将一个用户控件中的选定项传递给其他两个用户控件,这两个控件又作为 NewUnitOfWork 控件(或窗口)的内容交换。
如果每个控件都有自己的 ViewModel,我们需要将选定的值从一个 ViewModel 传递到下一个等等。
我已经使用全局变量(通过“服务”)在单用户情况下工作。然而,这存在并发问题,这不是一个好的解决方案。这是低于标准的。
我在这个论坛上多次看到过将 ViewModel 作为另一个 ViewModel 的成员的建议。虽然这解决了当前的问题,但我认为这违反了 MVVM 模式。另一个 ViewModel 不是 ViewModel 应该直接具有的 UI 相关功能。
所以。有没有人找到一种干净的 MVVM 兼容方法来完成此类事情?
干杯
I have been using the MVVM Light Toolkit to help learn the MVVM pattern. However, I have not been able to solve the problem of usercontrols within controls scenario.
For example, in a Timesheet application, lets say we have a control called NewUnitOfWork. When it first loads, a panel with a ListBox with a list of projects is loaded as the Content of the NewUnitOfWork. The user clicks on one. A new panel is swapped in with a ListBox containing the possible tasks for that project. A task is selected and a new panel is loaded which will contain controls to input data for the chosen task of the chosen project.
So, we have the selected item in one usercontrol being passed to the other two user controls, which are, in turn swapped in as the Content of the NewUnitOfWork control (or window).
If each control has its own ViewModel, we need to pass the selected value from one ViewModel to the next etc.
I have got it working in a single user situation using global variables (via a "service"). However, there are concurrency issues with that and it is not a good solution. It's sub-par.
I have seen many times the suggestion on this forum to have on ViewModel as a member of another ViewModel. Whilst this solves the problem at hand, I believe it is a violation of the MVVM pattern. Another ViewModel is not UI-related functionality that the ViewModel shoule be directly.
So. Has anyone found a clean MVVM-complying way to do this sort of thing?
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请始终记住,MVVM 只是一种模式,它旨在帮助您分离 UI 和逻辑。如果“违反模式”有助于提高应用程序的可测试性或可维护性,请不要害怕。
如果您有一个复杂的 UI,拥有一个带有多个子 ViewModel 的主 ViewModel 会非常方便。主 ViewModel 可能负责处理顶级 UI 控件并协调子 VM,而其他 ViewModel 则负责与 UI 子区域进行通信。
此外,如果您有一个具有多个嵌套 UI 层的非常复杂的 UI,您可以实现一个基础架构来自动将所有事件从主虚拟机级联到子虚拟机。
当然,您可以尝试使用更先进的 MVVM 框架之一。例如,Catel 实现了相当全面的模型来解决嵌套虚拟机的此类情况。
Please always keep in mind that MVVM is just a pattern and it is designed to help you separate your UI and logic. Do not be afraid to “violate the pattern” if it helps to increase testability or maintainability of the application.
Having a master ViewModel with several child ViewModels is very handy if you have a complex UI. The main ViewModel may be responsible for handling the top level UI controls and for coordination of the child VMs, while other ViewModels are responsible for communication with the sub regions of your UI.
Moreover, if you have a really complex UI with the multiple nesting UI layers, you can implement an infrastructure to automatically cascade all the events from master to child VMs.
And of cause, you may try to use one of the more advanced MVVM frameworks. For example Catel implements pretty comprehensive model to resolve such situations with nested VMs.
我没有看到 ViewModel 引用其他 ViewModel 的问题(根据我使用 TreeView 的经验)。查看有关 TreeView 和 MVVM 的任何文章。您将看到每个节点都是一个 ViewModel,它引用子节点的集合,这些子节点是 ViewModel。尝试在没有 VM-VM 引用的情况下做到这一点将是一场噩梦。
乔什·史密斯
http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx
I don't see a problem with ViewModels referencing other ViewModels (based on my experience with TreeViews). Have a look at any article about TreeView and MVVM. You will see that each node is a ViewModel, that references a collection of child nodes, which are ViewModels. Trying to do that without VM-VM references would be a nightmare.
Josh Smith
http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx
我一直在使用以下设置:
一个“主”虚拟机,其中一个“集合”虚拟机和一个“详细”虚拟机作为嵌套属性。
主 VM 绑定到用作主从形式的视图。这个主从视图由另外两个视图组成。
我发现它是一个非常简洁的设置,因为它允许我将搜索条件放在主视图(模型)中并保持其他视图(模型)的干净。
我不明白这会如何打破这种模式。
I have been using the following setup:
A 'master' VM with a 'collection' VM and a 'details' VM as nested properties.
The master VM is tied to a View that is used as a master-detail form. This master-detail View is composed from two other Views.
I find it a very neat setup because it allows me to put search criteria in the master View(Model) and keeps the other View(Model)s clean.
I can't see how this would break the pattern.