Search code examples
silverlightprismmef

MEF/PRISM Silverlight


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?


Solution

  • 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.