Odd behavior with regions

Topics: Prism v4 - Silverlight 4
Oct 24, 2010 at 11:17 PM

I'm trying to figure out an issue I'm having with Regions.

I have a UserControl that registers 3 regions in my main silverlight app.

<telerik:RadRibbonBar Grid.Row="0" IsMinimized="True" Margin="0"
                <telerik:QuickAccessToolBar x:Name="RootToolbarRegion" Regions:RegionManager.RegionName="RootToolbarRegion" common:QATBRegionAdapter.ItemContainerStyle="{StaticResource QATBRegionStyle}"   >
                    <!-- ItemsSource="{Binding QATBButtons}" ItemTemplate="{StaticResource QATBTemplate}" -->
                <telerik:ApplicationMenu x:Name="RootApplicationMenuRegion" Regions:RegionManager.RegionName="RootApplicationMenuRegion" >
        <sdk:TabControl Grid.Row="1" Grid.RowSpan="2" HorizontalAlignment="Stretch" Margin="0" Name="RootContentRegion" VerticalAlignment="Stretch"
                        Regions:TabControlRegionAdapter.ItemContainerStyle="{StaticResource CustomTabItemStyle}"
In a module, I try to register with the regions. 
This code works fine.
            IRegion _rootContentRegion = _regionManager.Regions[GlobalRegionNames.RootContentRegion];
            _rootContentRegion.Add(_module1RootTemplateView, ReflectionUtility.GetPropertyName(() => _module1RootTemplateView));
However, the RootContentRegion is the only region I see when I break in so this code fails.


//IRegion _rootToolBarRegion = _regionManager.Regions[GlobalRegionNames.RootToolbarRegion];


//_rootToolBarRegion.Add(_rootToolBarView, ReflectionUtility.GetPropertyName(() => _rootToolBarView));


However, what really doesn't make sense, if I run this code,



_rootToolBarRegion = _regionManager.RegisterViewWithRegion(GlobalRegionNames.RootToolbarRegion, typeof(RootToolBarView


Then it successufully registers the view with the Region. If the region isn't there, how does RegisterViewWithRegion find it and register the view? Or conversely, if the region is present, then when I set a break point, why can't I see the region in the regionManager? 

Oct 25, 2010 at 6:43 PM
Edited Oct 25, 2010 at 6:51 PM


There are two techniques in Prism for UI Composition. The first one is View Injection , while the other is named View Discovery, and both of them can be used for composing the UI of your application.

However, they are used for different situations. For more information on this topic, you could take a look at the following documentation section on MSDN: UI Composition, see the When to Use View Discovery vs. View Injection section.

That said, when you use the Add method in the Region class, you are using View Injection. This technique requires for an existent region in the Region Manager. So, if you receive an exception when adding views, it might be possible that the region has not been added to your Region Manager.

It may occurs because:

  1. The view which contains the region that you are using is not present in the Region Manager.
  2. The UI control which is exposed as a region does not derive from System.Windows.Controls.ContentControl, System.Windows.Controls.Primitives.Selector, System.Windows.Controls.ItemsControl or Silverlight TabControl.
    Therefore, you will need to write your custom region adapter. For more information on this topic you could check the following documentation section on MSDN: How to: Create a Custom Region Adapter

On the other hand, when you use the RegisterViewWithRegion method in the RegionManager class, you are using View Discovery. In this case, this technique does not require to create regions previously, since this allows you to set up a relationship between a region's name and the type of a view.

From the MSDN documentation: View Discovery... when a region is created, the region looks for all the ViewTypes associated with the region and automatically instantiates and loads the corresponding views.

Therefore, I would recommend you to review which technique fits better in your scenario and after that it might be necessary to create a custom region adapter for your UI control.

Hope this helps.

Fernando Antivero

Oct 25, 2010 at 7:19 PM

Ok, I guess the confusion on my part is thinking that the RegionAdapter was for implementing a style template when things get added.

You are correct, the Region that's showing up is in a Content control.  The other regions are in 3rd party controls.



Oct 25, 2010 at 11:32 PM

Having time to research this, the control I'm putting the the region in does inherit from ItemsControl.

I tried creating a RegionAdapter anyway but still it's not showing up in the RegionManager. 

It's a part of the Sample I posted on the other issue.


RootShellTemplate.xaml is the file where Regions are being defined.

The RootToolbarRegion & RootApplicationMenuRegion are the regions that aren't showing up.