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 t开发者_如何学Pythonabitem "BenchRegion" I want another instance
How do I do this?
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.
精彩评论