简体   繁体   中英

Loading a prism module view from the shell, using MEF

I have a shell project which is loading modules in my bootstrapper into a tab control in my shell's main view.

I have just implemented a close button on my tab items which now poses the question of how do I reload module views from the shell?

Tried using

moduleManager.LoadModule("ModuleA");

but this only works when the module is first loaded. When I call the above it loads and initializes the module, displaying the view. If I then close the view again, the 2nd time I try this it doesn't reshow the view (I'm guessing it doesn't reinitialize the module as it's already loaded).

So, I though I thought about using something along the lines of the following in my shell:

var moduleAView = ServiceLocator.Current.GetInstance<ModuleAView>();
regionManager.Regions["TabRegion"].Add(ModuleAView);
regionManager.Regions["TabRegion"].Activate(ModuleAView);

Trouble with this approach is how does my shell know about the type ModuleAView in line 1? I don't have a reference to module A and don't want to add one. I thought about a common interface that ModuleAView would implement, that could be shared amongst the module and the shell but I got a composition error when trying to use the ServiceLocator.GetInstance approach as above.

Many thanks for your help.

PS This is the module A module code I tried.

[ModuleExport(typeof(ModuleA), InitializationMode = InitializationMode.OnDemand)]
[Module(ModuleName="ModuleA")]
public class ModuleA : IModule
{
    private IRegionManager _regionManager;

    [ImportingConstructor]
    public ModuleA(IRegionManager regionManager)
    {
        this._regionManager = regionManager;
    }

    public void Initialize()
    {
        // add the search view to the region manager.
        this._regionManager.RegisterViewWithRegion("TabRegion", typeof(Views.ModuleAView));
    }
}

Was probably thinking of this along the wrong track.

Rather than attempting to show a module's view from the shell I publish an event from the shell which moduleA module subscribes to. Then I can decide what view to show in the module itself.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM