ViewModel code still executes when view not in region?

Topics: Prism v4 - Silverlight 4
Nov 11, 2010 at 4:54 PM

So I've got the whole MEF / PRISM / SL thing setup great. Modules load, views are injected into the appropriate regions, cross module communication works great, but I've run into a small caveat and I'm not sure how to solve it.

When I remove a view from a region the code in the ViewModel (MVVM) for the removed view is still executing (timers, events ext.). My regions are ContentControls and not ItemControls. Is this expected behavior? It seems to me like it might be and perhaps I just don't fully understand the execution model for Prism. Will I just have to add code to my ViewModel for the removed view to check if it is currently in the region? Does anyone know what I'm talking about even? :)


Nov 11, 2010 at 5:51 PM
Edited Nov 11, 2010 at 5:59 PM


In Prism there are two techniques for stop displaying a view in a particular region. The first one is calling the Deactivate method and the other is calling the Remove method in the Region class. The two techniques causes the same result in the UI. But when you deactivate a view, the view collection do not remove the reference to that view unless you use a particular region behavior named RegionMemberLifetimeBehavior.

If you need more guidance on this topic, you could take a look at the UI-Composition Quickstart as well as in the Stock Trader RI.

Other possible reason for this is that your application keeps a reference to the view in some place after removing it.

I think that you can find the following forum thread interesting: How to Remove A View From Memory When Closed

If you continue experiencing this situation, could you please share the code for removing views in your application? 

Hope this helps.

Fernando Antivero