Prism Modularity: This topic shows how to create a modular WPF application using the Prism library. The samples show how to code, discover and initialize modules.
Creating modules: Modules are classes that implement the IModule interface. Declarative attributes can be used to name modules, control initialization, and define dependencies.
Registering modules: Modules can be registered in the following ways:
- Directly in code: Modules can be directly registered in the module catalog in the application code. Using this approach, you can use conditional logic to determine which module should be included in your application. Modules added in the code are referenced by the application instead of being loaded at run time.
- Using configuration: Prism can register modules with the module catalog by loading a configuration file. Declaring the modules in configuration allows the modules to be loaded and initialized independently of the application.
- Using directory inspection: A directory can be specified and inspected to load assemblies in the directory and discover modules.
- Registering module dependencies: Modules can have dependencies on other modules. Prism provides dependencies management, including cyclic dependencies and duplicate module detection.
Initializing modules: Prism supports the following two initialization modes:
- When available: Modules can be initialized as soon as they are available. Modules downloaded the application are initialized during startup. Modules set to download in the background are initialized immediately after downloading completes.
- On-demand: Modules can be initialized when the application code requests it. Modules downloaded in the background start downloading when the application requests the module and then they initialize immediately after downloading completes.
In this example, I have taken 3 class libraries and one WPF Application. See the screen below.
Here every class library project is implementing “IModule”. See the code below.
- using Microsoft.Practices.Prism.Modularity;
- using Microsoft.Practices.Prism.Regions;
- namespace Module1
- {
-
-
-
- public class Module1 : IModule
- {
-
- #region Constructors
-
-
-
-
- public Module1(IRegionManager iRegionManager)
- {
- this.iRegionManager = iRegionManager;
- }
-
- #endregion
-
-
- #region IModule Members
-
- public void Initialize()
- {
- this.iRegionManager .RegisterViewWithRegion("Module1",typeof(Views.Module1Control));
-
- }
-
- #endregion
-
- #region Instance Fields
-
-
-
-
- private IRegionManager iRegionManager;
-
- #endregion
-
- }
- }
Initializing Modules: Here I have initialized modules in every IModule Implementation class, see the code below.
-
- public void Initialize()
- {
- this
- .iRegionManager
- .RegisterViewWithRegion("Module3Control1", typeof(Views.Module3Control));
-
- this
- .iRegionManager
- .RegisterViewWithRegion("Module3Control2", typeof(Views.Module3Control2));
- }
Registering Modules: Here I have registered all modules in Bootstrapper class, see the code below.
-
- protected override void ConfigureModuleCatalog()
- {
- ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;
- moduleCatalog.AddModule(typeof(Module1.Module1));
- moduleCatalog.AddModule(typeof(Module2.Module2));
- moduleCatalog.AddModule(typeof(Module3.Module3));
- }
-
- Finally, I have read all Modules in shell.xaml file, see the code below.
- <ItemsControl Grid.Row="0"
- Grid.Column="0"
- prism:RegionManager.RegionName="Module1" />
- <ItemsControl Grid.Row="0"
- Grid.Column="1"
- prism:RegionManager.RegionName="Module2" />
- <ItemsControl Grid.Row="1"
- Grid.Column="0"
- prism:RegionManager.RegionName="Module3Control1" />
- <ItemsControl Grid.Row="1"
- Grid.Column="1"
- prism:RegionManager.RegionName="Module3Control2" />
See the screen shot below.