在 Prism/Silverlight 中创建视图模型的多个实例

发布于 2024-10-07 07:49:06 字数 529 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天冷不及心凉 2024-10-14 07:49:06

您需要获取容器,并且对于视图模型的每个实例,您需要使用 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 first IUnityContainer.RegisterType<>()). You can think of ResolveType<>() 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文