Skip to main content

Module Manager

  • 3 minutes to read

ModuleManager provides API for integrating modules into an application and configuring navigation.

ModuleManager is available via the static ModuleManager.DefaultManager property.

The ModuleManager provides the following functionality:

Module Registration

Before displaying a module, it is necessary to register it. You can register or unregister modules using the ModuleManager.Register and ModuleManager.Unregister methods respectively.

Use the ModuleManager.GetModule method to retrieve a module registered in a particular region.

ModuleManager.DefaultManager.Register(regionName: "RegionA", module: moduleA);
ModuleManager.DefaultManager.Unregister(regionName: "RegionA", key: "ModuleA");
ModuleManager.DefaultManager.GetModule(regionName: "RegionA", key: "ModuleA");

Injecting and Removing Modules

You can show a registered module in a target region with the ModuleInjection.Inject method.

ModuleManager.DefaultManager.Inject(regionName: "RegionA", key: "ModuleA", parameter: null);

The ViewModels inside your modules can support the ISupportParameter interface. In this case, you can define the parameter for the ModuleManager.Inject method - it passes to the ISupportParameter.Parameter property of your module’s ViewModel. See Passing data between ViewModels (ISupportParameter) for more details.

To remove a module from the UI, use the ModuleInjection.Remove method.

ModuleManager.DefaultManager.Remove(regionName: "RegionA", key: "ModuleA", raiseViewModelRemovingEvent: true);

The last parameter controls whether the ViewModelRemoving event is raised (that is, if removing can be canceled).

To get the state of a module, use the ModuleManager.IsInjected method.

ModuleManager.DefaultManager.IsInjected(regionName: "RegionA", key: "ModuleA");

To remove all modules from UI, use the ModuleInjection.Clear method. Note that this method does not invoke raising the ViewModelRemoving event, so removing modules cannot be canceled.

ModuleManager.DefaultManager.Clear(string regionName);

Events

To control modules and navigation, ModuleManager provides a set of events. You can access all the events via the ModuleInjection.GetEvents method.

The code sample below demonstrates the events that are available for a particular region. Note that all events are weak.

ModuleManager.DefaultManager.GetEvents(regionName: "RegionA").Navigation += OnRegionANavigationChanged;
ModuleManager.DefaultManager.GetEvents(regionName: "RegionA").ViewModelRemoving += OnRegionAViewModelRemoving;
ModuleManager.DefaultManager.GetEvents(regionName: "RegionA").ViewModelRemoved += OnRegionAViewModelRemoved;

void OnRegionANavigationChanged(object sender, NavigationEventArgs e) { }
void OnRegionAViewModelRemoving(object sender, ViewModelRemovingEventArgs e) { /*e.Cancel = true;*/ }
void OnRegionAViewModelRemoved(object sender, ViewModelRemovedEventArgs e) { }

To subscribe a particular view model to events provided by ModuleManager use the following.

class VM {
    VM() {
        ModuleManager.DefaultManager.GetEvents(viewModel: this).Navigated += OnNavigated;
        ModuleManager.DefaultManager.GetEvents(viewModel: this).NavigatedAway += OnNavigatedAway;
        ModuleManager.DefaultManager.GetEvents(viewModel: this).ViewModelRemoving += OnRemoving;
        ModuleManager.DefaultManager.GetEvents(viewModel: this).ViewModelRemoved += OnRemoved;
    }
    void OnNavigated(object sender, NavigationEventArgs e) { }
    void OnNavigatedAway(object sender, NavigationEventArgs e) { }
    void OnRemoving(object sender, ViewModelRemovingEventArgs e ) { /*e.Cancel = true;*/ }
    void OnRemoved(object sender, ViewModelRemovedEventArgs e) { }
}

Regions

The Module Manager provides the ModuleInjection.GetRegion method to get a Region by its name. To get all the regions from a particular ViewModel, use the ModuleInjection.GetRegions method.

See Also