MVVM-Light WP7 - 交换虚拟机上的视图或访问以前的虚拟机而不是导航查询字符串参数?
有没有一种方法可以使用相同的视图模型在不同视图之间简单地交换?
我希望能够让用户导航到同一视图模型的不同视图。
根据我到目前为止找到的示例,我使用导航服务在视图之间切换,将 ID 传递到 URI,以便目标 VM 知道要加载什么记录。
不同的渲染是通过绑定在 XAML 上的帮助器转换对象来完成的,因此虚拟机基本上是模型和一些导航功能的薄包装。
另一种不同的导航方法是在同一视图上使用不同的渲染,并折叠未使用的视图直到使用它们,但这会使视图更加复杂,并且会将不同的渲染从导航堆栈中取出。
由于虚拟机在视图之间几乎相同,因此仅交换视图而不是基于 ID 存储/重新加载对象似乎更有意义。
操作假设是每个视图需要一个单独的虚拟机,或者至少每个视图需要一个单独的虚拟机实例,并且每个虚拟机无法访问前一个虚拟机的上下文,除了导航查询字符串上传递的内容之外参数。
回顾一下: 1) 我可以通过导航交换虚拟机上的视图而不丢失数据上下文吗?
或者 2) 我可以在导航到活动时访问之前的虚拟机吗?
更新: 更多信息 ->) 从 Mix11 Deep Dive Navigation 示例中 - 使用在导航查询上传递的 ID 作为键从 SimpleIOC 检索 VM,并将 DataContext 设置为其。这类似于存储/检索存储中的数据对象并根据键检索它。这是在目标页面代码隐藏的 OnNaviged 事件处理程序中。
Is there a way to simply swap between different views using the same view-model?
I'd like to be able to let the user navigate to different views of the same view model.
Based on the examples I've found so far, I'm using a navigation service to switch between views, passing in an ID to the URI, so that the target VM knows what record to load.
The different renderings are accomplished through helper conversion objects bound on the XAML so the VMs are basically a thin wrapper around the model and some navigation functionality.
A different approach to navigation would be to have the different renderings on the same view and collapse the unused views until they are used but that would make the view much more complex and would take the different renderings out of the navigation stack.
Since the VM is almost identical between views it would seem to make more sense to just swap out the views rather than to store/reload the object based on IDs.
The operating assumption is that you need a separate VM for each view, or at least a separate instance of a VM for each view, and each VM has no access to the context of the previous VM except what is passed through on the navigate query string params.
To recap:
1) Can I swap views on a VM with navigate without losing data context?
or
2) Can I get access to the previous VM on a navigate to event?
Update:
More info
->) From the Mix11 Deep Dive Navigation sample - retrieve the VM from SimpleIOC using ID passed on the nav query as the key and set the DataContext to it. This is similar to storing / retrieving the data object in storage and retrieving it based on the key. This is in the OnNavigated event handler in the target page codebehind.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
多个视图不需要单独的 ViewModel 实例,您可以很乐意在多个视图之间共享相同的 ViewModel(例如单例)。但是,您必须注意,更改此共享 ViewModel 的属性也会影响其他视图。
此外,您还可以将模型保存为单例,并让多个 ViewModel 绑定到此实例。
因此,对于您的问题:
您将必须导航到视图,但这可以绑定到单例 ViewModel。
这取决于你的所有 ViewModel 是否都是单例。但你想要吗?就我个人而言,我认为最好共享模型。
Multiple views do not need separate instances of the ViewModel, you can quite happliy share the same ViewModel (e.g. a singleton) between multiple views. However, you have to be aware that changing the properties of this shared ViewModel affects the other Views as well.
In addition you could also hold your Model as a Singleton and let multiple ViewModels bind to this instance.
So for your questions:
You will have to navigate to a View but this can be bound to a singleton ViewModel.
Well depends, if all your ViewModels are singletons you can. But do you want it? Personally I think it is better to share the Model then.