1

Hi I have a Silverlight application I've designed using MVVM patterns and Prism.

It has a number of modules that I would only like to load once the user has authenticated.

To this end I have created a login module, that takes the clients credentials and handles authentication. Once the user is authenticated I would then like MEF to load the rest of the modules. My problem is how to accomplish this?

The module catalog is defined in xaml and all modules except the login module are set to OnDemand initialization. As far as I'm aware modules can only be loaded from the shell, which doesn't work in my case as the call for them to load would come from the loginmodules viewmodel.

Any ideas would be greatly appreciated, thanks.

James
  • 101
  • 2
  • 7
  • Sounds like something I was looking for: http://stackoverflow.com/questions/6344949/silverlight-galasoft-mvvm-light-mef-loading-xap – Rumplin Sep 13 '11 at 14:56
  • @James You will have to do it manually. Normally you load modules manually or via catalog(in your case) inside Bootstrapper. Now you need to do this when authentication happened. – katit Sep 14 '11 at 05:50

1 Answers1

2

Prism provides an IModuleManager which you can import through MEF or Unity and then use to load modules.

    [ImportingConstructor]
    public MyViewModel(IModuleManager moduleManager)
    {
        this.moduleManager = moduleManager;
        this.moduleManager.LoadModule("ModuleA");
    }
Sebastian
  • 316
  • 1
  • 3
  • 12
  • Yeah I had seen the IModuleManager but I'm unsure of how to call it. If I use code similar to yours above I have to add a IModuleManager to my view to call the VM and as such the modulemanager is null. How do I get around this? – James Sep 14 '11 at 10:47
  • You wouldn't use the IModuleManager implementation in your View since your View should not contain code behind when using the MVVM pattern. You typically use it in your ViewModel. To get the IModuleManager via composition, your ViewModel must be created by MEF AND NOT via "new ViewModel", otherwise your injected object (IModuleManager) is null. – Sebastian Sep 14 '11 at 19:56
  • Thanks! Of course, a little tweaking to my view behind code and it works like a charm. – James Sep 15 '11 at 10:14