how to use IRegionMemberLifetime without MEF?

Topics: Prism v4 - Silverlight 4
Feb 1, 2011 at 3:28 AM



I have read this awesome article

I am now using Silverlight + Prism, first I have this:

public class ActionViewModel : IRegionMemberLifetime {

     public ActionVIewModel() {}
     public bool KeepAlive { get { return true; } } 

and then a simple view


public partial class ActionsView:UserControl
       public ActionsView()

      public ActionViewModel ViewModel {get;set;} 


In my Module Initialize, I have the following

Container.RegisterType<object, ActionsView>(ViewNames.DriverManagement.DriverActionView);

Then I use RegionManager.Regions["RegionName"].RequestNavigate( ... ) to request that view

The navigation works, but the ViewModel constructor is always hit when I navigate away from it and come back to this view. That means, every time I request this view, a new instance is created, even KeepAlive returns true.

So, did I miss anything here?

Thanks a lot!

Feb 1, 2011 at 4:33 PM


Take into account that when you are navigating from one view to the other, Unity is resolving your views.

Since you register ActionView in the container without specifying any LifetimeManager (that is in charge of controlling how the container the scope of a registration), each time you navigate to a view, the container is resolving a new instance of that view. The property marked with the [Dependency] attribute, in this case the view model ActionViewModel is also being resolved as a new instance, when Unity performs Property Injection. This is the reason why the ActionViewModel constructor is called every time you request the view during navigation.

In case your scenario needs to always use the same instance of the ViewModel, you need to register it like this to use a Singleton scope: 

Container.RegisterType<ActionViewModel>(new ContainerControlledLifetimeManager()); 

You can read more about Unity here, more specific the "Using a LifeTime Manager with the RegisterType Method" section.

I hope you find this information useful.


Miguel Bronzovic


Feb 1, 2011 at 5:41 PM
Edited Feb 1, 2011 at 6:04 PM

Hi, that's a great help

I am now using the ContainerControllerLifetimeManager to register my view, they really get created again when I request  the view second time.

But there are one further question:

1) If I return TRUE in KeepAlive, the view stays in the RegionManager.Regions["name"].Views, I can't simply RequestNavigate it again, instead, I need to call GetView and Activate it

2) If I return FALSE in KeepAlive, the view doesn't stay in the Views collection, so next time I request it, it will execute RequestNavigate, but the Container doesn't create a new instance of that view. Why?


I found a similar issue here

but that guy was using MEF, but I am not

The ContainerControlledLifetimeManager seems to ignore the KeepAlive value, as long as it is there, the instance just stays somewhere.


many thanks!