RegionManager does not contain nested region issue

Apr 1, 2015 at 5:25 AM
Hi,

I have a wpf application using prism and mef.
In my Shell I have define ModuleRegion.
In moduleRegion, when I load ModuleA, ModuleAMainView is loaded. ModuleAMainView has nested region LeftRegion and RightRegion.
ModuleAMainViewModel is implementing INavigationAware interface.

When I try to access LeftRegion and RightRegion in OnNavigateTo function:-

var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
var cregion = regionManager.Regions["LeftRegion"];

I get exception that region is not present.

regionManager contains only those regions which are defined in Shell.
Also if I try to call these line in UserControlLoaded event then I can see that regionManager has all the nested regions.

How to access these regions in OnNavigateTo functions.

Regards,
Sharda.
Apr 1, 2015 at 10:30 AM
I'm afraid you can't access the nested regions in OnNavigateTo, it is too early in the view lifecycle of the region. Regions are defined with an attached property and the implementation uses a DelayedRegionCreationBehavior that has to wait until the Visual Tree is created to determine what kind of element it is being attached to so that it can select the appropriate region adapter. And basically the visual tree does not come into being until after construction of your view class and before the Load event fires. So the Load event is the only reliable place to know that any contained regions have been created and the RegionManager is aware of them.

The way I typically deal with these kinds of things that are dependent on waiting for view load is to use a CallMethodAction behavior in the view to call a "Loaded" or "Initialized" method in the ViewModel based on the Loaded event in the view (tied in with an EventTrigger on the behavior). If what you need to do depends on the arguments passed to the OnNavigatedTo method, then store those parameters in a ViewModel variable when it gets called, and do the handling in the method called when Loaded fires.
Apr 2, 2015 at 10:27 AM
Hi BrianNoyes,

"The way I typically deal with these kinds of things that are dependent on waiting for view load is to use a CallMethodAction behavior in the view to call a "Loaded" or "Initialized" method in the ViewModel based on the Loaded event in the view (tied in with an EventTrigger on the behavior). "

I have never tried this solution. Could you please demonstrate it with some sample lines of code.

Regards,
Sharda
Apr 15, 2015 at 5:50 PM
Codeplex does not seem to want to let me paste in XAML code snippets here - probably sees as a cross-site scripting attempt or something.

Shoot me an email brian.noyes at gmail.com and I can send you a snippet.
Thanks
Brian
Apr 21, 2015 at 9:27 AM
Hi BrianNoyes,

I mailed you. Please check.

Regards,
Sharda.
Aug 25, 2015 at 11:31 AM
I tried fetching the "Loaded" event, but for me the region is still not there! Also, the attached property "RegionManager.RegionManager" is not set. What could be wrong? It seems I get the Loaded event too early...
<UserControl>
   <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <ei:CallMethodAction MethodName="OnLoaded" TargetObject="{Binding}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <TabControl prism:RegionManager.RegionName="TabRegion" prism:RegionManager.RegionManager="{Binding MyRegionManager}">
    
My UserControl belongs to the "MainRegion".
Aug 25, 2015 at 12:42 PM
I'm afraid I don't know what the issue is then, but it still sounds like a race condition in terms of when the region manager becomes aware of the region and when you are trying to navigate. If you can put together a small simple repro project that you could send, post an issue to our new location at http://github.com/PrismLibrary/Prism with a link to download the repro and we can take a look.