Modules

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

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");

Module Creation

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

    <ContentPresenter dxmvvm:UIRegion.Region="RegionA" ContentTemplate="{StaticResource ModuleTemplate}"/>
    

    When you inject the Module1, MIF creates the ModuleViewModel instance and passes it to the target control (for example, to the ContentPresenter.Content property).

  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.

    <ContentPresenter dxmvvm:UIRegion.Region="RegionA"/>
    
    //This code is a sketch for demonstration purposes.
    class ViewDataTemplateSelector : DataTemplateSelector {
        string Module1ViewTemplate = "
    <DataTemplate>
        <ModuleView/>
    </DataTemplate>";
        public override DataTemplate SelectTemplate(object viewModel) {
            if(viewModel is ModuleViewModel)
                return new DataTemplate(Module1ViewTemplate);
            return null;
        }
    }
    targetContentControl.ContentTemplateSelector = new ViewDataTemplateSelector();
    
  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