多个实例 ViewModel 和 ViewModelLocator
我使用 Unity 构建了一个 ViewModelLocator,并成功地将其与单例 ViewModel 实例一起使用。例如:
public class ViewModelLocator
{
private static readonly UnityContainer Container;
static ViewModelLocator()
{
Container = new UnityContainer();
if (ViewModelBase.IsInDesignModeStatic)
{
//Design Time Data Services
Container.RegisterType<IMyServiceServiceAgent, DesignMyServiceServiceAgent>();
}
else
{
//Real Data Services
Container.RegisterType<IMyServiceServiceAgent, MyServiceServiceAgent>();
}
Container.RegisterType<TreeViewViewModel>(new ContainerControlledLifetimeManager());
}
public TreeViewModel ViewModel
{
get
{
return Container.Resolve<TreeViewModel>();
}
}
}
ViewModelLocator 被定义为 App.xaml 中的资源:
<Application.Resources>
<ResourceDictionary>
<VMS:ViewModelLocator x:Key="ViewModelLocator" d:IsDataSource="True"/>
</ResourceDictionary>
</Application.Resources>
这允许我绑定到任何视图中的 ViewModel,如下所示:
DataContext="{Binding TreeViewModel, Source={StaticResource ViewModelLocator}}" d:DataContext="{d:DesignInstance IsDesignTimeCreatable=False}"
我的问题是如何在多个实例中维护相同的模式(和可混合性)相同的视图模型?
我在这篇文章中找到了我想要做的事情的参考 如何拥有多对“View-ViewModel”? 但它没有讨论实现的细节。
我想要做的是为不同的数据树拥有这些视图/视图模型对的多个实例,允许在它们之间进行复制和粘贴等,但无法思考如何使用容器来满足 ViewModelLocator 中的特定实例?
我假设我需要某种 ViewModel 集合(按照上面提到的帖子),但是如何向 Unity 容器注册该集合以及如何在视图中绑定到该集合?
非常感谢任何帮助。
I have built a ViewModelLocator using Unity and have been successfully using it with singleton ViewModel instances. For example:
public class ViewModelLocator
{
private static readonly UnityContainer Container;
static ViewModelLocator()
{
Container = new UnityContainer();
if (ViewModelBase.IsInDesignModeStatic)
{
//Design Time Data Services
Container.RegisterType<IMyServiceServiceAgent, DesignMyServiceServiceAgent>();
}
else
{
//Real Data Services
Container.RegisterType<IMyServiceServiceAgent, MyServiceServiceAgent>();
}
Container.RegisterType<TreeViewViewModel>(new ContainerControlledLifetimeManager());
}
public TreeViewModel ViewModel
{
get
{
return Container.Resolve<TreeViewModel>();
}
}
}
The ViewModelLocator is defined as a resource in App.xaml:
<Application.Resources>
<ResourceDictionary>
<VMS:ViewModelLocator x:Key="ViewModelLocator" d:IsDataSource="True"/>
</ResourceDictionary>
</Application.Resources>
Which allows me to bind to the ViewModel in any of the Views as follows:
DataContext="{Binding TreeViewModel, Source={StaticResource ViewModelLocator}}" d:DataContext="{d:DesignInstance IsDesignTimeCreatable=False}"
My question is how do I maintain the same pattern (and the blendability) with multiple instances of the same ViewModel?
I have found reference to what I am looking to do in this post
How to have multiple pairs "View-ViewModel"? but it does not go into the specifics of implementation.
What I want to be able to do is have multiple instances of these Views/ViewModel pairs for different data trees allowing copy and paste between them etc but cannot think how to cater for specific instances in the ViewModelLocator using the container?
I am assuming I need some kind of collection of ViewModels as per the post mentioned above, but how do I register that collection with the Unity Container and how do I bind to that in the View?
Any help is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的情况下,我所做的仍然是为视图保留一个 ViewModel,但有另一个 ViewModel 来保存可以更改的数据。
例如,如果我有一个显示用户信息的 UserView 控件,则我有一个通过 ViewModelLocator 绑定到该视图的 UserViewModel。我还有一个 UserModel 类,可以根据当前正在查看/编辑的用户进行更改。该 UserModel 类继承自 ViewModelBase 并由 UserViewModel 类通过属性公开。在应用程序的其他地方,例如,如果选择了用户,我将 UserViewModel 的 User 属性设置为我想要在 UserView 中显示的 UserModel。
What I did in your situation was to still have a single ViewModel for the view, but have another ViewModel that holds the data that can change.
For instance, if I have a UserView control that displays user information, I have a single UserViewModel bound to that view through the ViewModelLocator. I also have a UserModel class that can change depending on the current user being viewed/edited. This UserModel class inherits from ViewModelBase and is exposed by the UserViewModel class through a property. Somewhere else in the application, if a user is selected for instance, I set the UserViewModel's User property to be the UserModel that I want to be displayed in the UserView.