我应该如何对同一数据实现多个视图?
我有一个基于 MVVM/WPF 的应用程序,可以同时打开多个工作区(基本上包含 TreeView),在同一数据树上显示和操作。我需要能够独立地展开和折叠以及选择每个工作区中的项目,并且我希望能够在两个工作区之间拖放项目(例如,用于移动或复制项目)。我还需要数据虚拟化,因为只有在第一次展开树项时才会加载数据(在两个视图中)。还有一个工作区范围的详细信息视图,显示当前具有焦点的工作区中所选项目的详细信息。工作区特定的项目扩展和选择也必须以编程方式进行。
您能否为包含这些功能的基于 MVVM 的应用程序设计一个 Ruff 设计?我应该为每个工作区创建一个单独的 ViewModel 树并实现模型-ViewModel 映射器吗?或者我应该只有一棵带有两个视图的 ViewModel 树?详细信息视图如何知道当前选择了什么以及哪个工作区具有焦点? ...
因此,重新表述我的问题:每个视图中显示的模型实例实际上是相同的实例。但 ViewModel 也应该相同吗?或者更好:我能逃脱惩罚吗?
另外:是否有一个我可以研究的开源应用程序具有大部分这些功能? Prism 或任何其他基于 MVVM 的框架可以为我完成大部分工作吗?
I have a MVVM/WPF based application that can have multiple workspaces (basically containing a TreeView) open at the same time that display and operate on the same data tree. I need to be able to expand and collapse and select the items in each workspace independently and I want to be able to drag and drop items between the two workspaces (e.g. for moving or duplicating items). I also need data virtualization in that the data is only loaded (in both views) when a tree item is expanded for the first time. There is also one workspace-wide details view that shows the details of the selected item of the workspace that currently has the focus. Workspace specific expansion and selection of items must also work programatically.
Could you layout a ruff design for a MVVM based application that embraces theses features? Should I create a separate ViewModel tree for each workspace and implement a Model-ViewModel mapper? Or should I have just one ViewModel tree with two Views? How does the details view know what is currently selected and what workspace has the focus? ...
So to rephrase my question: The Model instances displayed in each View are actually the same instances. But should the ViewModels be the same too? Or better: Could I possibly get away with that?
Also: Is there an open source application I could study that has most of these features? Could Prism or any other MVVM based framework do most of that work for me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
View 和 ViewModel 之间存在直接关联。视图显示了模型的可视化表示,由 ViewModel 托管和“格式化”。
由于每个视图上都有不同的模型(数据),因此您需要有多个 ViewModel 实例来托管每组不同的数据。
现在的问题是:您的 ViewModel 之间是否共享一些实体或对象?
如果是,它们会在您的应用程序生命周期内发生变化吗?您想在 TreeView 中实时查看这些变化吗?
然后,您有两个选择:
第二个解决方案比第一个解决方案更纯粹,因为模型应该是 POCO 并且不应该实现一些面向管道的接口。 ViewModel 应该处理管道。
然而,它的设计比较复杂,并且 90% 的情况下,您最终会说“来吧,只有一个小界面不会有什么坏处”:)
对于您的详细信息视图。您可以使用中介器(MVVM Light Toolkit 中的 Messenger)在 TreeViewModel 和 DetailViewModel 之间进行通信,该中介器基本上是一个低耦合的面向事件的组件。或者使用注入到所有 ViewModel 中的主管并使用事件通知它们。
说到 MVVM 框架,对于您所描述的常见架构,我会推荐 MVVM Light Toolkit。
There is a direct correlation between View and ViewModel. The View shows a visual representation of the Model, hosted and "formatted" by the ViewModel.
Since you will have different Model (data) on each View, you need to have several instances of your ViewModel hosting each set of different data.
Now the question is: do your ViewModels share some entities or objects between them ?
If yes, could they change during your application lifetime, and do you want to see these changes in realtime in your TreeViews ?
You then have two options:
The second solution is more pure than the first one because Models should be POCO and shouldn't implement some plumbing oriented-interface. ViewModel should handle the plumbing.
However it's more complicated to design, and 90% of the time, you will end-up saying "come on, just one little interface doesn't hurt" :)
For your details view. You can communicate between your TreeViewModel and your DetailViewModel using a mediator (Messenger in the MVVM Light Toolkit), which is basically a low-coupled event-oriented component. Or use a supervisor which is injected in all your ViewModel and notify them using events.
Speaking of MVVM framework, and for common architecture like the one you are describing, I would recommend the MVVM Light Toolkit.
也许我遗漏了一些东西,但是同一个 View / ViewModel 的 2 个实例加载不同的数据是不可能的吗?
Maybe I am missing something, but is it not possible to have 2 instances of the same View / ViewModel loaded with different data?