You can't unload an entire module, though. You can dynamically load one, but not unload one. For more information read Loading Modules on Demand in the following article: http://msdn.microsoft.com/en-us/library/dd458911.aspx
Assumption: I'm going to start off with the assumption that when you say you want to unload a module, you really just want all of its views to be removed from your application.
First off, let's talk about how your modules work and how they show their content on your shell.
Your shell will have one or more Regions that your modules can then register views with when they are initialized.
public MyFirstModule : IModule
{
IRegionManager _mgr;
public MyFirstModule(IRegionManager mgr)
{
_mgr = mgr;
}
public void Initialize()
{
_mgr.RegisterViewWithRegion("MainRegion", typeof(MyView));
}
}
What you can do, though, is change your module initialization to track views that have been registered with this module and unload them when appropriate.
public MyFirstModule : IModule
{
IRegionManager _mgr;
IEventAggregator _agg;
IUnityContainer _container;
public MyFirstModule(IRegionManager mgr,
IEventAggregator agg,
IUnityContainer container)
{
_mgr = mgr;
_agg = agg;
_container = container;
}
List<object> Views = new List<object>();
public void Initialize()
{
_mgr.RegisterViewWithRegion("MainRegion", () =
{
MyView view = _container.Resolve<MyView>();
//Track this view so I can remove it if needed
Views.Add(view);
return view;
});
_agg.GetEvent<UnloadModuleRequest>.Subscribe(RemoveViews,
ThreadOptions.UIThread);
}
private void RemoveViews(string moduleToUnload)
{
//If this is true, that means the Shell is asking
//me to unload, so I will remove any views I've
//registered from any regions they were registered to
if(moduleToUnload == "MyFirstModule")
{
foreach(object view in Views)
{
_mgr.Regions["MainRegion"].Remove(view);
}
}
}
}
That way your Shell can publish an event called UnloadModuleRequest
(of type CompositePresentationEvent<string>
) and have any module listening unload any views it has registered.
I hope this is close to what you are wanting.