Menu in a module

Topics: Prism v2 - Silverlight 2
Mar 20, 2009 at 11:40 AM
I put my main application menu in a module, so that it is loaded after successful login. I have noticed that this menu stops working after other views are activated in my application, is this correct behaviour? Should menus be in modules, or embedded directly as user controls in the Shell?
Mar 23, 2009 at 12:48 PM
Edited Mar 23, 2009 at 12:51 PM
I was having problems similar to this where modules would just stop functioning all the sudden.  What you may want to check is that your module isn't being garbage collected because there is no more strong references to it.  The simplest way I've found to check for this is just to do something like this in the module class
Debug.WriteLine(String.Format("{0} object has been garbage collected.", this.ToString()));

Of course  you can substitute a logger or some other way of getting the message but its functional while debugging.  The Debug class also doesn't have issues with silverlight.  If your module is being disposed early, theres a couple ways to keep the garbage collector off of it.  For a small module that you need the whole application lifetime an easy way to handle this is to register it in the Unity Container in the bootstrapper by overriding the ConfigureContainer() method of the bootstrapper like
protected override void ConfigureContainer()
Container.RegisterType<Modules.ISomeModule, Modules.SomeModule>(new ContainerControlledLifetimeManager());   
The new ContainerControlledLifetimeManager() parameter has the unity container create a singleton reference to the registered type for its lifetime.  Which in most cases will be the whole applications lifetime.  For larger modules that you didn't want to hold in memory the entire lifetime of the application this probably isn't the best method but for a small menu module that is going to used for most of the applications life anyway its a fairly easy solution.

Hopefully that helps.
Mar 23, 2009 at 4:58 PM



As this article from the documentation explains: “Composite applications typically compose their user interfaces (UIs) from various loosely coupled visual components, otherwise known as views, that are defined in the application's modules.”

The purpose of defining views in modules and then show them in a Shell (in this case using regions as a placeholder), is that you can choose what component should be added to the Shell without depending on its content. Therefore, it is not recommended to embed a control directly to the Shell.


In your particular scenario, I believe the best approach is placing your menu in a module’s view and loading your MenuView in a region. The kind of control that should be declared as the region to hold the menu depends on your specific needs (step 3 of this article explains this concept better). The rest of the views in your application should be loaded into other regions, which depend on your application’s specifics.


Perhaps you could check out the Commanding Quickstart for Silverlight source code, which declares an ItemsControl as a region. The example as a single button to the view which simulates a menu.


For hierarchical or more complex kind of menus, you may create a MenuService instead of a region, that is in charge of adding menu commands to the UI, and each module uses this service to populate the menu at startup.


Please let me know if this helps.


Damian Schenkelman