Region not registering with regionmanager when ContentRegion is contained within a BusyIndicator

Topics: Prism v2 - Silverlight 4
May 18, 2010 at 4:23 PM

Hi, I have a problem when I have a ContentRegion defined within a BusyIndicator. Basically, the RegionManager does not register the region and so we have an exception throw when you try:

regions[RegionConstants.MainRegion]

            var regionManager = Container.Resolve<Microsoft.Practices.Composite.Regions.IRegionManager>();

            Microsoft.Practices.Composite.Presentation.Regions.RegionManager.SetRegionManager(shellView.MainContentRegion, regionManager);
            Microsoft.Practices.Composite.Presentation.Regions.RegionManager.UpdateRegions();

 

or

 

            var regionManager = Container.Resolve<Microsoft.Practices.Composite.Regions.IRegionManager>();

            Microsoft.Practices.Composite.Presentation.Regions.RegionManager.SetRegionManager(shellView, regionManager);
            Microsoft.Practices.Composite.Presentation.Regions.RegionManager.UpdateRegions();

 

but the regions property is still showing zero count. As soon, as I remove the BusyIndicator, the region is registered and everything works. Any ideas? Thanks,.

 

Developer
May 19, 2010 at 4:58 PM
Edited Jun 20, 2010 at 4:11 AM

Hi,

If I understood your scenario correctly, you are trying to add a region inside a Silverlight BusyIndicator’s content, but the region isn’t being added to the RegionManager. I created a working sample (which you can download here) in which I add a region to a BusyIndicator’s BusyContent.

Please note that if you are setting the region in the BusyIndicator’s BusyContentTemplate property, it won’t be added correctly, since the default RegionManagerRegistrationBehavior can’t find regions inside DataTemplates. You can find more information about this in this thread.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

May 19, 2010 at 6:03 PM

Hi Guido: thanks for the reply. I declared the region declaratively in the page inside the content template of BusyIndicator. So, there is a problem. I also noticed that some blogs mentioned you can manually wire using AddToRegion in RegionManager, but I can't find this method in IRegionManager or the implementation. Seems to be a method that was depreciated. I will have a look at your working example. Thanks again.

 

May 19, 2010 at 8:36 PM

Hi Guido:

It works with you use:

regionManager.RegisterViewWithRegion(...)

but, when you try to do anything like

IRegion region  = regionManager.Regions["MainRegion"];

region.Add(ShellPresenter.View);
region.Activate(ShellPresenter.View);

it won't work because the Regions collection is always empty.  To test this, I used the HelloWorld sample you sent me and I ran it with BusyIndicator commented out and then with it  uncommented.

When BusyIndicator was commented out, when I hit the breakpoint in overridden InitializeModules() method in Bootstrapper, I get one ActiveRegion in Container.Resolve<IRegionManager>().Regions.

When I re-run with BusyIndicator uncommented, it says no results <Empty>.

 You would think that it would be pretty common use case to have busy indicator wrapping most of the shell and thereby wrapping at least one region. Anyways, thanks for the sample, as it allowed me to see exactly what my problem was.

You see, I have a menu on the shell that loads views from various modules into a main region (also in the shell). So, I need to have better control than simply calling RegisterViewWithRegion. I need to be able to deactivate and remove views from regions, and I think this can only be done with IRegion instances. My app is working, but I may have to move the busyindicator to a section of the screen where it doesn't contain a region. Unless someone has another solution.

 

 

Developer
May 21, 2010 at 7:41 PM

Hi,

The “MainRegion” isn’t being added to the Regions collection inside the RegionManager because the behavior responsible for doing so (RegionManagerRegistrationBehavior.cs in Microsoft.Practices.Composite.Presentation.Regions.Behaviors) can’t find the RegionManager for that region. This happens because the control containing the region (an ItemsControl in my example) doesn’t have a parent. The behavior looks upwards through the logical tree to find the RegionManager.RegionManagerProperty attached property, which is usually in the shell; but since the ItemsControl doesn’t have a Parent property, it stops there and returns null, as if there were no RegionManager.

A possible workaround for that would be adding the BusyContent to the WPF Logical Tree. Another option would be to create a custom RegionAdapter to expose the BusyIndicator itself as a region.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi