Back to Devexpress

Modules

wpf-118615-mvvm-framework-mif-modules.md

latest2.6 KB
Original Source

Modules

  • Apr 01, 2021
  • 2 minutes to read

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.

Basic Information

The IModule interface is shown below.

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

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

csharp
ModuleManager.DefaultManager.Inject(regionName: "RegionA", key: "Module1");
ModuleManager.DefaultManager.Navigate(regionName: "RegionA", key: "Module1");
ModuleManager.DefaultManager.Remove(regionName: "RegionA", key: "Module1");

Module Creation

The Module class provides several constructors.

csharp
/*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")
  1. The first approach does not contain information about the View, and it is set at the target control level, for example:

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

  3. The third approach is similar to the second one. You can use it if you do not have access to View types.

  4. Use the last approach if you do not have access to ViewModel types.

See Also

IModule

Module