在 Prism/Silverlight 中创建视图模型的多个实例
我有一个 prism/silverlight 视图,它映射到我的 shell 选项卡控件中的 tabitem。 看起来像这样。
<sdk:TabControl>
<sdk:TabItem Header="User Portfolio" Regions:RegionManager.RegionName="MainRegion" />
<sdk:TabItem Header="Benchmark Portfolio" Regions:RegionManager.RegionName="BenchRegion" />
</sdk:TabControl>
该视图由数据网格、文本框和按钮组成,数据网格映射到视图模型中的可观察集合,单击按钮时,文本框中的文本将添加到数据网格(以及相应的集合)。
现在,我想声明此视图-视图模型对的多个实例。也就是说,在 tabitem“MainRegion”中我想要一个实例。在tabitem“BenchRegion”中我想要另一个实例
我该如何做?
I have a prism/silverlight view and it is mapped to a tabitem in a tab control of my shell.
It looks like this.
<sdk:TabControl>
<sdk:TabItem Header="User Portfolio" Regions:RegionManager.RegionName="MainRegion" />
<sdk:TabItem Header="Benchmark Portfolio" Regions:RegionManager.RegionName="BenchRegion" />
</sdk:TabControl>
The view consists of a datagrid,textbox and a button such that the datagrid maps to an observablecollection in the viewmodel and when the button is clicked, the text in the textbox gets added to the datagrid(and the corresponding collection).
Now, I want to declare multiple instances of this view-viewmodel pair. That is, in tabitem "MainRegion" I want one instance. In tabitem "BenchRegion" I want another instance
How do I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要获取容器,并且对于视图模型的每个实例,您需要使用
IUnityContainer.ResolveType<>()
来初始化实例(确保首先注册您的类型IUnityContainer .RegisterType<>()
)。您可以将ResolveType<>()
视为 Prism 形式的构造函数。然后,对于每个视图,您需要将数据上下文设置为该视图的初始化视图模型。编辑 我应该注意,这是针对 Prism 2.0 我知道在 Prism 4.0 中还有 Unity 之外的替代方案。
You need to get the container, and for each instance of the view model you need to use
IUnityContainer.ResolveType<>()
to initialize the instance (Make sure you register your types firstIUnityContainer.RegisterType<>()
). You can think ofResolveType<>()
as Prism's form of a constructor. Then for the each view you need to set the datacontext to your initialized view model for that view.Edit I should note that this is for Prism 2.0 I know that with Prism 4.0 there are alternatives to unity.