Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 634
Gus Cavalcanti Avatar asked Dec 12 '25 22:12

Gus Cavalcanti


2 Answers

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.

like image 184
NJE Avatar answered Dec 16 '25 21:12

NJE


You can remove a View by calling Regions's Remove method.

public void RemoveViewFromRegion(string viewName, string regionName, object defaultView)
    {
      IRegion region = regionManager.Regions[regionName];
      object view = region.GetView(viewName);
      region.Remove(view);
      region.Activate(defaultView); 
    }
like image 45
skjagini Avatar answered Dec 16 '25 23:12

skjagini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!