0
votes

I am working on a very large application using WPF, PRISM v4 and Unity.

The application is broken into several areas, each area has its own Scoped RegionManager.
After the Modules initialization I can see all the RegionManagers and all of them have all their defined regions and views.
When I activate one of the Views, which is the root of a Scoped RegionManager, some of the regions in that RegionManager are removed and I can't navigate to them.

I subscribed to the RegionManager.Regions.CollectionChanged event for the specific Scoped RegionManager and I see in the event callback callstack that it is originating from RegionManagerRegistrationBehavior.TryRegisterRegion, but I can't figure out what's causing the region to be removed.

I add every Scoped RegionManager that I create to the Unity Container for resolving it when needed during navigation, and I also call the RegionManager.SetRegionManager for each view.
The views are added to the regions using region.Add(view, name).

I'll try to give an outline of the Regions and Views in play:
MainView
-ViewA - Scoped RegionManager root view
---Region1
-----ViewB
-------Region2 -> This region is removed
----------ViewC - These 2 views are injected to this region
----------ViewD

ViewA has 2 instances, which is why a Scoped RegionManager is needed, the same view is injected to 2 regions (in the Main view above it) and shown in different areas of the application.

This problem is a little complicated to explain, but I hope someone can understand what I'm trying to show here.

Any help would be appreciated

1

1 Answers

2
votes

Ok, after much debugging I came up with a solution and I'm posting it here in case anyone else stumbles upon this problem.

What I saw when debugging the Prism code is that it looks for the RegionManager on the Host Control of the view that I try to navigate.
In my example above, ViewC is the view that is navigated, and ViewB is it's Host Control.
What I did in my module initialization is call SetRegionManager on both the view itself and it's Host Control (using LogicalTreeHelper.GetParent, as Prism does in TryRegisterRegion).
This can only be done after the view is added to the region, probably because that is when the LogicalTreeHelper knows how to find it's parent.

If anyone has a better solution I'd be happy to hear it.