LoadModule Unity WPF

Topics: Prism v4 - WPF 4
Mar 1, 2011 at 11:57 AM

Hi, I use prism v4.x with WPF and unity.

I've got the problem that the loading of one module takes a lot of time and i want to load in the background during some other views are shown.

All my module are registered in the ModuleCatalog like this:

 protected override void ConfigureModuleCatalog()
 {
  base.ConfigureModuleCatalog();
ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;
moduleCatalog.AddModule(typeof(GraphicsRegionModule.GraphicsTemperatureModule.GraphicsTemperatureModule));
}

protected override void ConfigureContainer()
{
base.ConfigureContainer();
Container.RegisterType<object, GraphicsRegionModule.GraphicsTemperatureModule.View.GraphicsTemperatureModuleView>(ViewNames.GraphicsRegionTemperatureGraphicsView, new ContainerControlledLifetimeManager());
}

[Module(OnDemand=false, ModuleName="Temperature")]
public class GraphicsTemperatureModule : IModule
{
        void IModule.Initialize()
        {
            //throw new System.NotImplementedException();
        }
}

To Load I tried the following:


container.Resolve<Microsoft.Practices.Prism.Modularity.IModuleManager>().LoadModule(Infrastructure.ADUView.ViewNames.GraphicsRegionTemperatureGraphicsView);

or

container.Resolve<Microsoft.Practices.Prism.Modularity.IModuleManager>().LoadModule("Temperature");

But nothing works (always the message unknown module). Can anyone give me a hint how it will work?



Mar 1, 2011 at 3:17 PM

Hi,

One possible way to achieve your requirement would be to configure your Module Catalog by simply overriding the ConfigureModuleCatalog method in your Bootstrapper like this:

protected override void ConfigureModuleCatalog()
{
   Type tempModule = typeof(GraphicsTemperatureModule); 
   this.moduleCatalog.AddModule(new ModuleInfo()
   {
       ModuleName = tempModule.Name,
       ModuleType = tempModule.AssemblyQualifiedName,
       InitializationMode = InitializationMode.WhenAvailable
   });
}

You can read more about modules in Chapter 4: Modular Application Development and you might find handy the Modularity QuickStarts provided with Prism. There you will find examples and explanations of possible ways to load modules in Prism.

Additionally, it could be helpful to know that the ModuleManager.LoadModule method can only load a module if you pass a ModuleInfo as the parameter. In case you pass a view object, a string or a type, a compilation error will be thrown. You can read more about the ModuleInfo members here.

I hope you find this information useful.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic

 

Mar 2, 2011 at 9:03 AM

Hi,

thank you now the loading of the module works (it throws no exeption anymore) but the viewing of the module still takes to much time. I set e breakpoint on the constructor of the viewmodel class of this module (I bind it with this.DataContext = viewmodel to the view) but this contructor is still called only when the view of this module is shown for the first time. Only the showing of this view for the first time takes so much time, every later call is much faster. This is the reason why I want to load the module during the start-up in the background. Is there a possibility to call the viewmodel-class during the module.Load process? Is there an other possibility to increase the showing of this view?

Can you please give me an other hint?

 

Thank you!

Mar 2, 2011 at 3:26 PM

Hi,

The behavior you're describing for the loading of the ViewModel and the View happens because you're using the View-First approach (that is to say, you're specifying the ViewModel as a dependency of the View). If you want to create the ViewModel before creating the View, you could use the ViewModel-First approach. You can read more about in the following links:

Take into account that there are some implications with using the ViewModel-First approach, so you should read carefully before deciding to implement it.

If the delay is caused by the fact that you need to retrieve information from a web service or something similar, you could follow another approach: you could have a service in your module that retrieves the necessary information, and once the information has been retrieved, it publishes an event indicating that the view should be loaded, thus avoiding the delay when loading the view. The ViewModel should obtain (through dependency injection) a reference to that service and access the data from there. This is possible because initializing a module does not necessarily imply that its views should be loaded.

You might find the following Prism MSDN chapters handy:

I hope you find this information useful.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic

 

Mar 2, 2011 at 3:51 PM

Thank you, this is much to read and it seems to be good stuff. I will work through it and give you a  response if you like.

 

Thanks a lot,

 

Stefan