MEF: Inject same view (new instance) multiple times to another region

Topics: Prism v4 - WPF 4
Feb 21, 2011 at 12:53 PM
Edited Feb 21, 2011 at 1:32 PM

Hello,

I am using PRISM 4 with MEF in WPF.  I have a view, which is defined as:

 

    [ViewExport(RegionName = RegionNames.MainRegion)] 
    [PartCreationPolicy(CreationPolicy.NonShared)]
    [RegionMemberLifetime(KeepAlive=true)]
    public partial class DocumentSelectionView : UserControl

 

On a button click (command), another View is loaded to the region, which contains another sub region. There I want to inject a _new_ instance of the DocumentSelectionView, leaving the existing (hidden) instance alive, so when going back it should be simply reactivated with latest state. 

I created a controller class to programmatically inject the new view to the region (which should be done multiple times with different configuration settings). 

How to get a new instance of the view including resolving all the types needed (initializing constructor)? 

 

region.Add(???) // how to get a new view instance resolved?

 


Best Regards

Andreas

Feb 21, 2011 at 1:32 PM
Edited Feb 21, 2011 at 1:32 PM

Hello,

got a step forward. Added an additional [Export] attribute to my view, so now I get the view through  ServiceLocator.Current.GetInstance<DocumentSelectionView>()

 [ViewExport(RegionName = RegionNames.MainRegion)] 
 [Export]
 [PartCreationPolicy(CreationPolicy.NonShared)]
 [RegionMemberLifetime(KeepAlive=true)]
 public partial class DocumentSelectionView : UserControl


Is it ok to have 2 export (ViewExport, Export) attributes at the view? 

Furthermore I figured out that the ViewModel haven't changed between the instances was caused by missing the following on the _ViewModel_

 [PartCreationPolicy(CreationPolicy.NonShared)]


Conclusion: With PartCreationPolicy NonShared on both the View and the ViewModel it seems to work - I get a new instance using ServiceLocator. 
Is that the right approach?