2
votes

I am new to MEF/Prism and Silverlight and I am not sure how to dynamically load modules into a ItemsControl. I basically want to be able to swap out dynamically loaded modules into the same region container. How can I do this using MEF/PRISM?

2
Modules aren't loaded into "Regions"! Regions are used to display "Views", which can be "discouvered" or "injected". Could you please reformulate your question and add more details? :) (by the way, the PRISM documentation along with the QuickStarts and the Reference Implementations are a good way to start ;) )AbdouMoumen

2 Answers

2
votes

A region to host several controls must be of type ItemsControl:

<ItemsControl 
    x:Name="MainToolbar" 
    cal:RegionManager.RegionName="{x:Static inf:RegionNames.MainToolBarRegion}">
</ItemsControl>

Views can be added and removed by code:

//add view if not already present
IRegion region = this._regionManager.Regions["RegionName"];

object view = region.GetView( "ViewName" );
if ( view == null )
{
    var view = _container.ResolveSessionRelatedView<IMyView>( );
    region.Add( view, "ViewName");
}

//remove
IRegion region = this._regionManager.Regions["RegionName"];

object view = region.GetView( "ViewName" );
if ( view != null )
{
    region.Remove( view );
}

Using this code you could also add the views by code besides using the Prism built-in navigation functionality.

3
votes

The QuickStart project in Prism does exacly what you ask for. It's called Modularity QuickStart :)

Also, have a look at this thread: Using Prism for navigation in Wpf application

View Injection QuickStart does set a view on change of ListView selected item so might be of some help too.