简体   繁体   English

使用MEF从shell加载棱镜模块视图

[英]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. 我有一个shell项目,它将我的引导程序中的模块加载到我的shell主视图中的选项卡控件中。

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? 我刚刚在我的标签项上实现了一个关闭按钮,现在提出了如何从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: 所以,虽然我想在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? 这种方法的问题是我的shell如何知道第1行中的ModuleAView类型? I don't have a reference to module A and don't want to add one. 我没有对模块A的引用,也不想添加模块。 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. 我想到了ModuleAView将实现的一个通用接口,它可以在模块和shell之间共享,但是在尝试使用上面的ServiceLocator.GetInstance方法时遇到了组合错误。

Many thanks for your help. 非常感谢您的帮助。

PS This is the module A module code I tried. PS这是我试过的模块A模块代码。

[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. 我没有尝试从shell显示模块的视图,而是从shell发布了一个事件,模块A模块订阅了该事件。 Then I can decide what view to show in the module itself. 然后我可以决定在模块本身显示什么视图。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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