Best way Initializing Module from Menu at run time

Aug 14, 2008 at 8:13 PM
hi.

I want to have a main menu that will initialize modules when the user selects an item.  In the bootstrapper, I add the modules

 

in IModuleEnumerator GetModuleEnumerator.  I don't want to initialize them until selected from the menu.  The command event takes me to the code behind, but I can use the  moduleLoader.Initialize because it only takes an array.  Should I be using an event that is subscribed to by the modules?

 

 

 

Aug 15, 2008 at 8:45 AM
Edited Aug 15, 2008 at 8:47 AM
A good idea would be create one central module that creates the menu items and attaches commands to them. The following XAML and viewmodel is a good startingpoint to implement the menu view components:

<Menu>
  <MenuItem Command="{Binding LoadModuleCommand}" CommandParameter="ModuleA" Header="Module A"/>
  <MenuItem Command="{Binding LoadModuleCommand}" CommandParameter="ModuleB" Header="Module B"/>
</Menu>

public class MenuViewModel {
  DelegateCommand<string> _loadModuleCommand;

  public DelegateCommand<string> LoadModuleCommand
  {
    get { return _loadModuleCommand; }
  }
}

When one of the commands is invoked it should load the module using the following piece of code:

public class MainModule: IModule {
  IUnityContainer _container;

  public MainModule(IUnityContainer container) {
    _container = container;
  }

  public void Initialize() {
    // Load the menu view and attach the viewmodel
  }

  // This handles the load module command logic
  void LoadModule(string moduleName) {
    ModuleInfo[] moduleInfo = _container.Resolve<IModuleEnumerator>().GetModule("ModuleName");
    _container.Resolve<IModuleLoader>().Initialize(moduleInfo);
  }
}

Be sure to mark the modules with StartupLoaded=false, otherwise they get loaded and the effect is lost.
Hope this helps.
Aug 15, 2008 at 1:44 PM
Thanks the Menu Module idea.  I like it.  I'm still having problems w/ ModuleLoader.  The moduleInfo variable errors out because _container.Resolve doesn't return an array.  I guess I could modify the CAG so it does take one value :) 

 // This handles the load module command logic
  void LoadModule(string moduleName) {
    ModuleInfo[] moduleInfo = _container.Resolve<IModuleEnumerator>().GetModule("ModuleName");
    _container.Resolve<IModuleLoader>().Initialize(moduleInfo);