wpfprismmodularitycag

How can I activate/deactivate a module's view after its initialization?


This relates to Composite Application Guidance for WPF, or Prism.

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

var module = moduleEnumerator.GetModule(moduleName);
moduleLoader.Initialize(new[] { module });

At the first time 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.

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

Thank you for your help!


Solution

  • You don't actually activate the module. You activate a view in a region. Take a read of this article.

    The Initialize method is only called the once for any module. The fact that you are seeing a view in the module being activated when you call LoadModule I would guess is due to the fact that the Initilalize method is registering a view with a region. This will activate the view. If you had more than one view then the last registered would be the active one.

    To Activate a view you need to call the Activate method of the region (assuming an injected IUnityContainer and IRegionManager)...

    // Get a view from the container.
    var view = Container.Resolve<MyView>();
    
    // Get the region.
    var region = RegionManager.Regions["MyRegion"];
    
    // Activate the view.
    region.Activate(view);
    

    Depending on the type of region control this will either replace the view that is there or add to it.