user638750
user638750

Reputation: 111

Prism: Loading Modules in to catalog after shell is created

Using either Unity or MEF can you load Modules after the bootstrapper creates the catalog? In other words have a button click load a module which was not known when the application started and the bootstrapper CreateModuleCatalog code has executed? I have not found a good example of this either in the documentation or through internet searches. Either it does not support this or I am just plain missing something. Everything I find loads up modules in the bootstapper only.

The basic WPF project I am trying to do a proof of concept is:

  1. Load up the application. It will load up some standard modules. The shell will be created and visible.
  2. User interaction will trigger the need to discover a new module, add it to the catalog and then expose it on the UI. I am not so concerned about how it discovers the modules yet, more so on how to load them. The discovery will mostly likely be querying a database, downloading required .dlls and then saving to a known directory.

I have a feeling that it is relatively simple and I have just been spinning my wheels trying to figure this out.

Upvotes: 4

Views: 5720

Answers (2)

Ash Shah
Ash Shah

Reputation: 11

I am new to Prism and I have had a similar issue. After much searching, I could not find any direct help. However, I have solved the issue in a different way. Here is the code below:

  1. Created a DelegateCommand Property in the viewmodel class (MasterViewModel) and on the event handler code adds and loads a new module in the Modulecatalog.

  2. Hooked it on to a button's click event on the View's xaml

    <Button Content="Button" Height="28" HorizontalAlignment="Left" Margin="8,0,0,0"
        Name="btnLoadModule2" VerticalAlignment="Top" Width="98" 
        prism:Click.Command="{Binding  DataContext.LoadModule2Command, ElementName=root}"/>
    
  3. Used the dependency injection to get the references of ModuleCatalog & ModuleManager

  4. On the click event code I added a Module in code(i.e. Module2) to the ModuleCatalog and used Module manager to load it.

    public MasterViewModel(IDataService dataService, IEventAggregator eventAggregator, IRegionManager regionManager
                            , IModuleCatalog moduleCatalog, IModuleManager moduleManager)
    {
        _dataService = dataService;
        _eventAggregator = eventAggregator;
        _regionManager = regionManager;
    
        // Get the data model from the data service.
        _model = dataService.GetModel();
    
        // Initialize the CollectionView for the underlying model.
        DataItemsCV = new ListCollectionView(_model);
        // Track the current selection.
        DataItemsCV.CurrentChanged += new EventHandler(SelectedItemChanged);
    
        // Initialize the commands.
        NavigateToViewCommand = new DelegateCommand<string>(NavigateToView);
        SyncViewCommand = new DelegateCommand<string>(SyncView);
    
        LoadModule2Command = new DelegateCommand<string>(LoadModule2);
        _moduleCatalog = moduleCatalog;
        _moduleManager = moduleManager;
    }
    
    void LoadModule2(string s)
    {      
        ModuleInfo module = new ModuleInfo()
                            { 
                                Ref="Module2.dll", ModuleName="Module2", 
                                ModuleType="Module2.ModuleInit, Module2, Version=1.0.0.0", 
                                InitializationMode= InitializationMode.WhenAvailable , 
                             };
        module.DependsOn.Add("Module1");
    
        _moduleCatalog.AddModule(module);
        _moduleManager.LoadModule("Module2");
    
    }
    

This technique can be used to load modules after the initial Shell initialisation. I must confess that Geoff's answer was slightly irrelevant.

Upvotes: 1

Geoff Cox
Geoff Cox

Reputation: 6152

Take a look at the Prism 4.0 Quickstart - Modularity with MEF for Silverlight.

This quickstart creates a catalog from XAML, but you can manually add entries to the module catalog and pass similar parameters. The only thing the module info class needs is the REF to the XAP file.

You can also look at the desktop version. This one finds DLLs containing modules in a directory and then loads them from disk. You could do the same thing by pointing to a known DLL at some file location.

Essentially, if you add the right module info into the ModuleCatalog, then demand loading of the module the DLL with be downloaded or loaded and MEF/Unity containers will have that module intialized.

Upvotes: 3

Related Questions