views:

1103

answers:

4

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!

A: 

You should have a ContentControl that will be your region. Then you will need to add all your modules to this region. When you click on the menu you should use Activate(...) method of the region in order to activate the particular module.

ligaz
A: 

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

compositewpf.codeplex.com/Thread/View.aspx?ThreadId=42862

[couldn't make this a hyperlink - "sorry, new users aren't allowed to add hyperlinks"]

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.

NeilE
how about to deactivate? What's your recommendation?
Gustavo Cavalcanti
There is no real way to deactivate. To 'deactivate' a view you would either activate another or maybe activate a blank view if the effect you are after is to blank the region.
NeilE
...or collapse the region if the intention was to hide the area.
NeilE
A: 

Does this mean when yuou activate module, then other modules that may be overlapped by it are set to Visibility.Collapsed?

AlvinfromDiaspar
A: 

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); 
    }
skjagini