Reputation: 123
I have a MEF/Prism composite WPF application and am having trouble getting views to show in regions in the following scenario:
The app has 2 windows. A main start up window with a button, when that button is clicked a new window (child window) is created and shown. The child window has some regions. When the app starts up I use the RegionManager.RegisterViewWithRegion
to register views for the child window.
Now, the first time the child window is created and shown I get the views injected and it works fine. But, when I close the child window then click the button on the main window to create and show a new instance of the child window, the window is created but no views are injected.
It is as if the RegionManager has lost its registrations or it can't find the regions in the child window the second time around.
Any idea why it is working this way?
Thank You.
Upvotes: 0
Views: 1208
Reputation: 11
I had the same problem (Unity/Prism 4). To solve it i remove the active view from the region of the childwindow and i remove the region in the regionManager before to closing the child window (OkButton and Cancel button). I'm not sure that is the good solution but it work. (my region is added to the region manager on the child window constructor)
Child Window Constructor
this.regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
RegionManager.SetRegionManager(this.ActionContent, this.regionManager);
RegionManager.SetRegionName(this.ActionContent, REGION_NAME);
this.regionManager.Regions[REGION_NAME].Add(this.sharedViewProperty.UIElement,
this.sharedViewProperty.ViewName);
this.regionManager.Regions[REGION_NAME].Activate(this.sharedViewProperty.UIElement);
OKButton_Click
this.regionManager.Regions[REGION_NAME].Deactivate(this.sharedViewProperty.UIElement);
this.regionManager.Regions.Remove(REGION_NAME);
Now I have a strange behavior when i set the RegionManager in my constructor. I have a Exception "Microsoft.Practices.Prism.Regions.Behaviors.RegionCreationException: An exception occurred while creating a region with name 'ChildRegion'. The exception was: System.ArgumentException: Region with the given name is already registered:"
When i insert this following line before to set the region manager, it work :
this.regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
int nbCountRegion = this.regionManager.Regions.Count();
RegionManager.SetRegionManager(this.ActionContent, this.regionManager);
RegionManager.SetRegionName(this.ActionContent, REGION_NAME);
Don't tell me why, i can't explain it...
Upvotes: 1