Prism: loading modules into the directory after creating the shell

Using Unity or MEF, can you load modules after bootstrapper creates a directory? In other words, click the β€œDownload” button for a module that was not known when the application started and ran the CreateModuleCatalog code to boot? I did not find a good example of this in either the documentation or the internet search. Either this does not support, or I just missed something. All I find is loading modules only in bootstapper.

The main WPF project I'm trying to do to prove the concept:

  • Download the app. It will load some standard modules. A shell will be created and visible.
  • Interaction with the user will make it necessary to open a new module, add it to the catalog and then open it in the user interface. I'm not really worried about how it detects modules, much less how to load them. Basically it will be searching for the database, loading the necessary .dll, and then saving to a known directory.

I have a feeling that it’s relatively simple, and I just turned the wheels trying to figure it out.

+4
source share
2 answers

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

This quick start creates a directory from XAML, but you can manually add entries to the module directory and pass similar parameters. The only thing the module information class needs is the REF for the XAP file.

You can also see the desktop version. This finds the DLLs containing the modules in the directory, and then loads them from disk. You can do the same by pointing to a known DLL in some place.

Essentially, if you add the required module information to ModuleCatalog, ask the module to load, a loadable or loaded DLL, and the MEF / Unity containers will have that module.

+3
source

I am new to Prism and I had a similar problem. After a long search, I could not find direct help. However, I solved the problem differently. Here is the code below:

  • The DelegateCommand property was created in the viewmodel class (MasterViewModel) and adds and loads a new module into the Modulecatalog in the event handler code.

  • Connected it to a button click event in xaml view

    <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}"/> 
  • Dependency injection used to get ModuleCatalog and ModuleManager links

  • In the click event code, I added a module to the code (e.g. Module2) in ModuleCatalog and used the 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 method can be used to load modules after initial Shell initialization. I must admit that Jeff's answer was a little irrelevant.

+1
source

Source: https://habr.com/ru/post/1341750/


All Articles