使用MEF从外壳加载棱镜模块视图 [英] Loading a prism module view from the shell, using MEF

查看:73
本文介绍了使用MEF从外壳加载棱镜模块视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Shell项目,该项目将Bootstrapper中的模块加载到Shell主视图中的选项卡控件中.

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?

尝试使用

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

这种方法的问题是我的外壳如何知道第1行中的ModuleAView类型?我没有对模块A的引用,也不想添加一个.我想到了ModuleAView会实现的通用接口,该接口可以在模块和外壳程序之间共享,但是在尝试使用上述ServiceLocator.GetInstance方法时遇到组合错误.

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.

非常感谢您的帮助.

PS 这是我尝试过的模块A模块代码.

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.

我没有尝试从shell显示模块的视图,而是从shell中发布了moduleA模块所预订的事件.然后,我可以决定在模块本身中显示什么视图.

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.

希望这会有所帮助.

这篇关于使用MEF从外壳加载棱镜模块视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆