How to activate a module after its initialize method has been executed?

Dec 11, 2008 at 7:33 PM

I have one "MainRegion" (SingleActiveRegion) in my shell. My various modules will be loaded into this main region. I can populate a list of available modules in a menu (a module with MenuView.xaml) and select them to load. On the click of the menu I do:

(MenuView.xaml.cs)
public partial class MenuView
{
    private readonly IModuleLoader moduleLoader;
    private readonly IModuleEnumerator moduleEnumerator;

    public MenuView(IModuleLoader moduleLoader, IModuleEnumerator moduleEnumerator)
    {
        this.moduleLoader = moduleLoader;
        this.moduleEnumerator = moduleEnumerator;
        InitializeComponent();
    }

    private void MenuItem_Click(object sender, RoutedEventArgs e)
    {
        var module = moduleEnumerator.GetModule(moduleName);
        moduleLoader.Initialize(new[] { module });

    }
...
}   

At the first time it all works OK, because the Initialize() methods of the modules are executed, but after Module1, Module2 and Module3 are initialized, nothing happens when I click to load Module2 again (Initialize() doesn't get executed anymore). I am sure my code is wrong as I shouldn't be calling Initialize(), just to activate a module previously initialized, but moduleLoader doesn't give me any other options...

My question: how can I activate a module on demand, after its initialize method has been executed?

Thank you for your help!

Dec 12, 2008 at 4:54 PM

Hi

 

The Module Loader service only initializes the modules that weren’t initialized (has a collection of all the modules that have been already initialized in order to not initialize the same module twice).

 

What you could do is have your on demand modules’ initializers subscribe to an event in the EventAggregator, but not show any view. Then your MenuView would initialize the module (which will really be initialized only the first time), and later publish that same event in the event aggregator, passing the moduleName as an argument (there’s an overload in the subscription method that will let the module’s initializer filter and get only the events related to itself).

This is when your module could reactivate (or add if it’s the first time) the views in the region.

 

private void MenuItem_Click(object sender, RoutedEventArgs e)

{

var module = moduleEnumerator.GetModule(moduleName);

moduleLoader.Initialize(new[] { module });

eventAggregator.GetEvent<ActivateModuleEvent>().Publish(moduleName);

}

 

I recommend you to avoid calling the Initialize method more than one time. The purpose of it is to be called only once during the module's loading process, to perform module configuration and set up. The Composite Application Library provides several ways to implement loosely coupled communication among different modules like:

·         Event Aggregator service to publish and subscribe to events

·         Commands that allow to handle user interface (UI) actions to bind the UI to the logic that performs the action

 

Please let me know if this helps.

 

Mariano Converti

http://blogs.southworks.net/mconverti

Dec 15, 2008 at 11:28 PM
Thanks Mariano. I will try this route and let you know how it goes.
I appreciate your help!
- Gustavo