Reputation: 21
I was referring to the thread View not connecting to ViewModel using MEF.
I have similar scenario but a few addons. The application is composed of several modules and the Shell has defined main regions. Each module has a view(userControl) and viewmodel and the views are loaded in the shell regions on demand. The problem is the Views are composed of child usercontrols each having their own ViewModel. So I chose to inject the child controls in a scope region defined in the view as I could have mulitple instances of the View.
But somehow I am not getting through using MEF.
The following is pseudo code and app structure.
1.Shell has Mainregion defined. Parent View(s) load in this MainRegion on demand.
2. Parent View which FooParent has a xaml where in I define a region.
<ContentControl prism:RegionManager.RegionName ="FooChildRegion" />
3. The FooChildUsercontrol code behind class is marked with [Export] attribute.
4. FooParentModule has a initialize method with a regionmanager importing constructor
5. Code in Initialize method of Module
IRegion foochildregion = this._regionManager.Regions["FooChildRegion"]; //This blows.
FooChildView fooChildView = new FooChildView();
IRegionManager allocationViewRegionManager = foochildregion .Add(fooChildView , null, true);
foochildregion .Activate(fooChildView );
The first line for getting the instance for FooChildRegion blows as the module is initialized on startup and the view is loaded on demand.
Any idea what I am missing here?
Upvotes: 0
Views: 750
Reputation: 21
I got a work around for this. In Parent I defined the ContentControl
as
<ContentControl x:Name="FooChildContentControl" />
Then in the Code behind of Parent Control I create a new instance of regionManager and set to Child control as under
IRegionManager regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
RegionManager rm = new RegionManager();
RegionManager.SetRegionManager(this.FooChildContentControl, rm);
RegionManager.SetRegionName(this.FooChildContentControl, "FooChildRegion");
rm.RegisterViewWithRegion("FooChildRegion", typeof(fooChildView ));
Upvotes: 1