Have a single instance in a region?

Topics: Prism v2 - Silverlight 4
May 27, 2010 at 5:04 PM

I have a Silverlight 4 app using PRISM. I have a region in a usercontrol that is loading a modular app. The modular app has the initialize as this:


       public void Initialize()
            _regionManager.RegisterViewWithRegion("SolutionRegion", () => _container.Resolve<Views.SolutionView>());

My application overall uses tabs to navigate. My problem is that each time I tab to the control that has the region, a new instance of my view is loaded. I need to load one instance and leave it there so the user can come back and not lose work. Any ideas?

May 28, 2010 at 9:43 PM


In order to keep the same instance of your view each time you resolve it through the container, you have to register it as a singleton instance.

In that case, your code would look like this:


public void Initialize()


_regionManager.RegisterViewWithRegion("SolutionRegion", () => _container.Resolve<Views.SolutionView>(new ContainerControlledLifetimeManager()));



You can find a deeper explanation in this thread.

If you register it as a singleton, depending on your scenario you should be careful with memory issues, as the view will be kept alive (probably along with the ViewModel and all the instances it keeps) until the application is closed.

If you are using a TabControl as a region following the Prism approach, that shouldn’t behave as it does now. You should be able to switch between tabs without losing the state of your view. In that case there is no need to register the view as a singleton instance, avoding memory issues. You could check the UI Composition quickstart to see an example of this behavior.

I hope you find this helpful.

Guido Leandro Maliandi