Removing Views from a Region Removes Views and Region

Topics: Prism v4 - Silverlight 4
Jun 27, 2011 at 5:45 PM

When I run this code to remove all views in my region it is also removing the region.  I do not want it to remove my region. Is there something extra that I need to do to keep my region alive?

 if (RegionManager.Regions.ContainsRegionWithName(AdminDockSiteRegionIdentifiers.RightDocked) == false)
                return;

            var region = RegionManager.Regions[AdminDockSiteRegionIdentifiers.RightDocked];
            
            
            foreach (var view in region.Views.Where(view => view != null))
            {
                region.Remove(view);
            } 
I would also like to mention that the region in question is a custom region adaptor I created for a docking framework. I have traced my custom region adaptor code and only see the region tearing down the view, nothing else appears to be functionally wrong.
Thanks in advance!
Developer
Jun 27, 2011 at 6:34 PM

Hi,

The region should be kept alive as long as the control it's mapped to is added in the visual tree. Also, the RegionManager should hold a reference to your region in its Regions collection.

You could check the code of your custom adapter to check if there's something in its code that might make the view that holds your region to be disposed when you dispose a child item in it. That could be the cause of this behavior.

I hope you find this helpful.

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

Jun 27, 2011 at 7:41 PM

Hi Guido and thanks for you quick reply.

I am still not seeing why this is removing the region from the RegionManager but it most definately is.

In my applicaiton I have a tab control within a separate document window (separate region) and to the right of this document window (region) I have a right docking region which is where I inject now two views.

Currently I am using the onselectionchanged event of the tab control situated in my document window and when fired, I test to see if the tab item's datacontext (viewmodel) being changed from/to supports a custom interface I wrote that allows me to call back through the tab item(s) related view model and request it to remove any related views from the docking region (ie, will call's a supporting method of the old tabitem's view model tabbing from) and then I call the the tab item's supporting interface to requestnavigate any views back into the docking region (within my custom interface's). 

I added two views instead of just one to the same and only removed one of the views. 

What I see is what I expect to see, the one view remains (does not implement IRegionMemberLifetime) while the other is removed and the Region remains which allows me to navigate the one I removed back into the region when I click back to the supporting tab item.

It's as if some code elsewhere is requesting that I remove this docking region but hangs on until all the views are removed. I have searched high and low but do not see where anything is trying to remove a region. 

   

 

 

Jun 27, 2011 at 9:56 PM

This is really killing me. I have spent the entire day trying to understand what is going on as it really makes no sense whatsoever.

In an attempt to isolate what the heck is going on I thought I would remove one of the two views from the docking region I am having issues with (see above) however the code below is not finding the named view which does not make sense for when I debug to that point and can see two views on the said region (named accordingly) however the GetView method is not locating the view in question, even though I can clearly see it within that region.

Can anyone explain what the heck I am missing ?

            var region = RegionManager.Regions[AdminDockSiteRegionIdentifiers.RightDocked];

            var vw = region.GetView("ApplicationPropertyContainerView");
            if(vw != null)
            {
                region.Remove(vw);
            }
Developer
Jun 28, 2011 at 2:58 PM

Hi,

The Region.GetView method retrieves a view that had been added with a specific name (by providing a name string in the Region.Add method). Therefore, if you haven't registered your views with a name string, the GetView method won't return anything.

If you can't debug this further to find a solution to your problem, you could upload a repro sample application (for example, to Skydrive) so that we can help you better understand what could be causing this undesired behavior.

I hope you find this helpful.

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

Jun 28, 2011 at 5:43 PM

Hi and thanks again for your support.

I am actually using the RequestNavigate to load my view (see code below) .  Is there any other way of registering the View without performing the Region.Add method?

   RegionManager.RequestNavigate(AdminDockSiteRegionIdentifiers.RightDocked,
                                            new Uri("ApplicationPropertyContainerView", UriKind.Relative));
Developer
Jun 28, 2011 at 6:25 PM

Hi,

If you register your view using navigation, the view will not be added using a name string, so you won't be able to retrieve it using the GetView method.

Currently, the way Prism offers to register views with a name string is to use the Add method.

As for alternative means of adding views to regions, you could follow the View Discovery approach, which involves using the IRegionViewRegistry.RegisterViewWithRegion method (or the IRegionManager.RegisterViewWithRegion extension method, which produce identical results). However, that method also doesn't offer the possibility of registering the view with a name string.

You can read more about this in the UI Composition chapter of the Prism MSDN documentation.

I hope you find this helpful.

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

Jun 30, 2011 at 3:22 PM

Hi Guido,

You were 100% bang-on.  As it turns out the Region was unloading because the Docking Area it was bound to was unloading.  I did not fully understand the behavior of the 3rd Party Docking Framework I am using and after consulting with the vendor I was informed that the docking areas do unload if you remove all windows.

As can be seen in this link, http://compositewpf.codeplex.com/discussions/263394 I created a check to see if this condition exists and then recreate both the unloaded docking area then create and bind a new region to it.  Seems to work just fine.

Many thanks for you help!