如何在MVVM Light中具有同一ViewModel的多个实例? [英] How to have multiple instances of the same ViewModel in MVVM Light?

查看:531
本文介绍了如何在MVVM Light中具有同一ViewModel的多个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将MVVM Light用于带有选项卡的WPF应用程序(有点像Internet Explorer).每个选项卡必须是几个唯一的View和View模型.我知道我的标签控件中将有多少个实例.

I'm using MVVM Light for a WPF application with tabs (a bit like Internet Explorer). Each tabs has to be a couple of unique View and View models. I know know how many instances I will have in my tab control.

由于MVVM Light的设计,我不能同时拥有ViewModel的多个实例(定位器中的静态属性).在codeplex网站上,我找到了以下解决方案:

Due to the design of MVVM Light I can't have multiple instances of a ViewModel at the same moment (static property in the locator). On the codeplex Website i found the following solution :

我处理此问题的方法是通过使ViewModelLocator中的ViewModel属性(例如MainViewModel的Main属性)在每次调用时都返回MainViewModel的另一个实例.这样,每个视图将具有MainViewModel的单独实例.

The way I handle this is by making the ViewModel property in the ViewModelLocator (for example the Main property for the MainViewModel) return a different instance of the MainViewModel each time it is called. This way each view will have a separate instance of the MainViewModel.

但是,这意味着解析给定窗口的DataContext会更加复杂(因为ViewModel不再保持静态).如果需要解析,则可以基于例如存储在View中的唯一ID在ViewModelLocator中构建查找表,或者使用IOC容器根据键实例化并解析ViewModel实例.

However, this means that resolving the DataContext of a given window is a bit more complex (since the ViewModel is not kept static anymore). If you need to resolve, you can build a lookup table in the ViewModelLocator based on a unique ID stored in the View for example, or use an IOC container to instantiate and resolve the ViewModel instances according to a key.

我了解我必须在每个视图中插入一个ID,我了解我必须在Locator中维护这些ID的列表,我了解我必须在定位器中进行查找才能将每个视图与正确的定位器相关联,但是我不知道如何将此ID从我的视图传递到定位器.我如何在自己的XAML中做到这一点?

I understand I have to inject an ID in each view, I understand I have to maintain a list of these IDs in the Locator, I understand I have to lookup in the locator to associate each view to the right locator, but I have no idea how to pass this ID from my view to the Locator. How can I do that in the XAML of my view ?

我希望我的问题很清楚,如果您需要更多详细信息,请告诉我.

I hope my question is clear enough, let me know if you need more details.

推荐答案

我曾经在我的博客上它适用于Windows Phone,但也适用于WPF. 设置与我的设置之间唯一的区别是,您需要从每个选项卡中获取唯一的ID,就像我导航到新视图时所获得的一样.

It's for windows phone but would work for WPF too. Only difference with your setup versus mine is that you need to get your unique ID from each tab, as I get mine when navigating to a new view.

因此,我建议您连接到制表符更改事件,然后在此时检索您的密钥. 不确定如何在视图中跟踪键,但是我想您可以使用它了,如果没有的话,您可以始终将其存储在控件的某些Tag属性中.

So what I would suggest is hook up to the tab changed event and retrieve your key at that point. Not sure how you keep track of your keys in the view, but I guess you have it available, if not you could always store it in some Tag property of the control.

最后但并非最不重要的是将数据上下文与所需的视图模型绑定,这是您可以执行的操作:

Last but not least is to bind the datacontext with the needed viewmodel, this is how you could do that:

tabControl.DataContext = ViewModelLocator.GetViewModel<***ViewModel>(tabKey);

TabControl是当前的活动选项卡(我猜应该在那设置datacontext吗?) TabKey是检索正确的对应视图模型所需的当前所选选项卡的唯一键.

TabControl being the current active tab ( I'm guessing the datacontext should be set on that? ) TabKey being the unique key for the current selected tab that is needed to retrieve the correct corresponding viewmodel

很抱歉使用高级伪代码,但是由于您没有添加当前解决方案的一些参考代码,因此有点猜测工作;)

Sorry for the high level pseudo code but because you didn't add some reference code of your current solution it's a bit guessing work ;)

这篇关于如何在MVVM Light中具有同一ViewModel的多个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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