wpf-118615-mvvm-framework-mif-modules.md
A module is a functional unit of an application. Regarding the Module Injection Framework ( MIF ), the module is a structure that binds a ViewModel to its View. Each module implements the IModule interface.
This topic describes how to register and control modules.
The IModule interface is shown below.
public interface IModule {
string Key { get; }
Func<object> ViewModelFactory { get; }
string ViewModelName { get; }
string ViewName { get; }
Type ViewType { get; }
}
MIF provides a quick IModule implementation - the Module class. Register modules with the ModuleManager.Register method.
ModuleManager.DefaultManager.Register(
regionName: "RegionA",
module: new Module(
key: "Module1",
viewModelFactory: () => new Module1ViewModel(),
viewType: typeof(Module1View)
)
);
You can show, activate, or remove a registered module by its key.
ModuleManager.DefaultManager.Inject(regionName: "RegionA", key: "Module1");
ModuleManager.DefaultManager.Navigate(regionName: "RegionA", key: "Module1");
ModuleManager.DefaultManager.Remove(regionName: "RegionA", key: "Module1");
The Module class provides several constructors.
/*1.*/ new Module(key: "Module1", viewModelFactory: () => new ModuleViewModel());
/*2.*/ new Module(key: "Module1", viewModelFactory: () => new ModuleViewModel(), viewType: typeof(ModuleView))
/*3.*/ new Module(key: "Module1", viewModelFactory: () => new ModuleViewModel(), viewName: "ModuleView")
/*4.*/ new Module(key: "Module1", viewModelName: "ModuleViewModel", viewName: "ModuleView")
The first approach does not contain information about the View, and it is set at the target control level, for example:
The second approach sets the ViewType parameter. In this case, MIF creates a special DataTemplateSelector for the target control that selects a view template based on the view Model.
The third approach is similar to the second one. You can use it if you do not have access to View types.
Use the last approach if you do not have access to ViewModel types.
See Also