Clearing Child Views from region and memmory

Topics: Prism v4 - WPF 4
Apr 25, 2012 at 1:43 PM
Edited Apr 25, 2012 at 1:44 PM

Hi all,

      I have a main region where in I load views. I load different views in the main region. The problem is that prism is keeping these views in memory and loading these views when I switch between views using RegionManager.RequestNavigate. For example if I load View-A in main region, then navigate to View-B in the same main region and then navigate back to View-A then the View-A is not newly created. It is the same view that was there before I navigated to View-B. 

So the issue is how to clear the Views from memory (when navigated from), so that prism cannot re-use those views. In the above example when I navigate to View-B, I want the View-A (and all its sub-views) to be completely destroyed, so that when I navigate back to View-A, I get a completely new instance of View-A (and its nested sub-views). 

Here is what I have tried

Downloaded prism 4.1 and used in MainRegion ClearChildViews="True" attribute like this on the main region


<ContentControl prism:RegionManager.RegionName="{x:Static g:RegionNames.MainRegion}" Grid.Column="2"


I also used the above ClearChildViews in each of sub-regions of main region but without success

I used RegionMemberLifetime attribute in each views as given below


    public partial class SContactMainView : UserControl {
        public SContactMainView() {


If anyone knows how to destroy existing views from a region and its nested-regions when a different view is loaded, please help me. 



Note: I had also posted same issue in StackOverflow forum, but its been more than a couple of days and I still not have had any response.

Apr 25, 2012 at 5:56 PM

Hi Nirvan,

I believe that the problem you are experiencing could be related to the fact that your view is decorated with the PartCreationPolicy attribute set to CreationPolicy.Shared (this can be seen in the code snippet you posted above.) Based on my understanding, this means that MEF will threat your view as a "singleton" and will always return the same instance of the view. Therefore, when performing a navigation request, it seems that the view is removed from the region; but when Prism asks for a new instance of the view MEF returns the same instance that was removed.

You can try decorating your view with the PartCreationPolicy attribute set to CreationPolicy.NonShared and check if this solves the problem you are experiencing.

I hope you find this useful,

Damian Cherubini

Apr 26, 2012 at 3:05 AM
Edited Apr 26, 2012 at 3:05 AM


You were indeed correct that the PartCreationPolicy.Shared was the causing the same view to be returned by the container. I changed it to CreationPolicy.NonShared and resolved my issue. Thank you very much.



Apr 26, 2012 at 4:45 AM

I have another related doubt/issue. After changing the PartCreationPolicy to Shared, I get the behavior I needed when switching views. But when I reload the same view, I still get the old instance of the view. For example, consider that I navigate to View-A. Now when I try to reload the View-A again, I still get the old instance of the View-A. In short switching different views produces new instances of view (exactly as I want), but reloading the same view returns the same instance. 

How do I reload a view with a new instance.



Apr 26, 2012 at 2:00 PM

Hi Nirvan,

Based on my understanding of your scenario, you may find the INavigationAware interface useful. As mentioned in the documentation, by implementing this interface, your view or view model can opt-in to participate in the navigation process.

This way if your view returns false in its IsNavigationTarget method (defined in this interface), you should be able to retrieve a new instance of the view. In other words this allows an existing (displayed) view or view model to indicate whether it is able to handle the navigation request.

You could find more information about this topic in the following chapter of the Prism documentation at MSDN:

I hope you find this handy,

Agustin Adami

Apr 27, 2012 at 1:56 AM


Thanks for the link, but I have already implemented INavigationAware interface. My problem is that I have nested region. When the parent region is Navigated from to another parent region, NavigatedFrom callback is not called for the child views. An example will make it clear.

I have Shell which contains MainRegion. I have a module ModuleA. The main view for ModuleA is ViewA which is loaded into Shell's MainRegion. ViewA contains a nested region RegionAA with view ViewAA. Now when I navigate from ModuleA to another module, the NavigatedFrom of ViewA is called, but the NavigatedFrom of ViewAA (contained inside ViewA) is not called. This suggests that the RegionAA and its view ViewAA is not cleared from memory, as a result of which I get RegionCreationException when I navigate back to ModuleA. What I was looking for is that when ModuleA is unloaded(NavigatedFrom), all its child views should be removed from memory (that is the NavigatedFrom of all child views inside ViewA should be invoked). But this doesn't happen despite of all the attempts that I tried (as mentioned previously).

So the question boils down to : What is the best way to dispose nested regions ?



Apr 27, 2012 at 9:42 PM

Hi Nirvan,

I reproduced this scenario based on your aforementioned descriptions. Also using the following attributes in my views:


And the IsNavigationTarget method defined to return false. As the result, when I navigated from ViewA to ViewB and back I didn't found any problem. On the other hand, I found that when navigating from ViewA to a different instance of ViewA a RegionCreationException was thrown:

An exception occurred while creating a region with name 'RegionAA'. The exception was: System.ArgumentException: Region with the given name is already registered: RegionAA

As you mentioned it seems that when re-navigating to a different instance of the same view the aforementioned exception is raised because Prism tries to register the RegionAA again before removing the previous one.

In my opinion, as a possible approach to work around this behavior you could try using Scoped regions which will allow a view to have its own instance of a region manager. However, Scoped regions currently are only available with view injection in Prism from out of the box, as you could find in the following work item:

There you could find different proposed approaches to use Scoped regions with the Region Navigation functionality.

Also, you can find more information about Scoped regions in the following link of the Prism documentation:

As a side note, about why the call of the OnNavigatedFrom method of the view is not called, take into account that this method wouldn't be called in the inner views if the navigation request is performed in the "MainRegion," and not in the inner ones.

Additionally, regarding this subject you could find the following thread interesting:


Agustin Adami

Apr 30, 2012 at 8:50 AM
Edited Apr 30, 2012 at 8:52 AM


Based on you suggestion, I decided to use scoped regions and I picked up the "" from Navigation and ScopedRegions . The Scoped region managers were instantiated when the view was marked with [ScopedRegionManager] attribute. Also, using the RegionManager attached property, it was possible to set the LocalRegionManager in the view model.

The only problem that I had was that the Child Views could not access the LocalRegionManager without manual wiring up. So I searched on net and found a RegionBehavior, which makes it easy for the Child Views to access the LocalRegionManager without any code. 

That resolves my issue. Thanks for all the help



Jul 18, 2012 at 7:29 AM

Hi Nirvan

Could you say a litttle more about the RegionBehavior you are referring to? Where can I find it and how do I use it? I am trying to do the same as you and would appreciate any help I can get.

Jul 19, 2012 at 7:43 PM

Hi Mfalcon,

I believe you con find the region behavior that is included in this blog post useful:

After registering this behavior in the Bootstrapper of your application, when a view or view model that implements the IRegionManagerAware interface is injected in a region, the behavior will automatically set its RegionManager property to the corresponding RegionManager for the view.

I hope you find this useful,

Damian Cherubini