Module Mgr no Loading on second call.

Topics: Prism v4 - Silverlight 4
Dec 20, 2010 at 11:36 PM

I have a Silverlight Prism app. and depending on the selection in the "tree" a "view" (xmal module) is displayed in the "mainRegion".  Simple enough.  When the user clicks "A", the A module is shown.  When the user clicks "B" the "A" region is removed from the "mainRegion" and the "B" region is shown. The problem comes in when the user again clicks "A", which has already been initialized which means when I call LoadModule("A"), region B is removed, and nothing is displayed. 

The code below is linked to a button that simply swaps the views, but removing one, and loading the other, except on the second time through, the Load("A") is called succesfully, but nothing is shown in the mainRegion, it is empty.  Yet the view resides in memory and in fact its timer (a variable associated with the view) is still firing away.

This code is linked to a button that simply swaps the views.  There is no errors generated, but the behavior is not correct.
object o = m_RegionManager.Regions["mainRegion"].Views.FirstOrDefault();
if (o != null)
	m_RegionManager.Regions["mainRegion"].Remove(o);
if (m_bTestModule)
	m_ModuleManager.LoadModule("A");
else
	m_ModuleManager.LoadModule("B");

m_bTestModule = !m_bTestModule;
If anyone knows a work around for this problem, please let me know.  Even more bizare is when the brower is stopped and restarted, the view is sometimes 
present even after a restart, which means the code persists between instations of the browser.  That is scary.
Thanks,
Chris
Developer
Dec 21, 2010 at 3:16 PM

Hi Chris,

The problem you're experiencing is probably because you're trying to load a module twice. As explained on the Modularity chapter of the Prism MSDN documentation, modules can only be loaded once, and cannot be unloaded. You might find more information about this in the following threads:

In order to achieve your scenario, you could use the Event Aggregator (more information on this chapter) to publish an event indicating that the views from certain module should be added. For example:

object o = m_RegionManager.Regions["mainRegion"].Views.FirstOrDefault();
if (o != null)
    m_RegionManager.Regions["mainRegion"].Remove(o);
if (m_bTestModule)
    eventAggregator.GetEvent<LoadViewToMainRegionEvent>().Publish("A");
else
    eventAggregator.GetEvent<LoadViewToMainRegionEvent>().Publish("B");

m_bTestModule = !m_bTestModule;

Then, in both of your modules respectively, you should subscribe to those events, and make them add their views to the Main Region. In order to do so, you could use Navigation or any of the UI Composition mechanisms provided by prism. You can read more about this in the following threads:

You might find the following QuickStarts useful to illustrate some of the concepts I've explained you:

As an additional note, you can only add and remove views from regions, as explained on the UI Composition chapter of the Prism MSDN documentation. If you need to place a region inside another region, the recommended approach is to use scoped regions (also explained in the aforementioned chapter), which implies adding a view that contains a region, inside another region (which also complies with the fact that you can only add views to regions).

I hope you find this helpful.

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

Dec 21, 2010 at 3:37 PM

Thanks for the reply Guido.  The event publishing solution also suggest that the views have subscribe to an "Unload" event which would free the objects currently associated with the view.

 

Chris

Developer
Dec 21, 2010 at 4:23 PM
Edited Dec 21, 2010 at 4:24 PM

Yes, that's a possibility. In your case, as you're removing the only active view in that region, it's not strictly needed to subscribe to an "Unload" event in your modules, and you could use the approach that you were using to remove the view from the region.

However, if you plan to design your solution so that it could be extended in the future to support adding and removing views dinamically, you could benefit from using an unload event to notify the modules that they should remove their views from the main region.

I hope you find this helpful.

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