RegionManager不包含我配置的区域

时间:2011-11-29 11:21:58

标签: wpf prism mef

我正在使用Prism,MEF(MEF Bootstrapper)和WPF。我是新手,我有一个非常简单的应用程序,我试图去上班。

我在主窗口(shell)中有两个区域可以正常工作。我可以在运行时在窗口中看到它们,我可以通过Shell窗口内的代码和其他窗口中的TheRegionManager.Regions [" regionName"]访问它们。为此,我使用: [进口] public IRegionManager TheRegionManager {private get;设置;}

现在我想对Application中的另一个窗口做同样的事情。我有两个区域,我从XAML配置它们,我做:

        TheRegionManager.RegisterViewWithRegion("AdsMainRegion", typeof(Ads));
        TheRegionManager.RegisterViewWithRegion("AdDetailsRegion", typeof(AdsDetail));

从Shell注册其他2个区域中的视图。这种方式有效,因为我可以在运行时看到区域中加载的视图。问题是我无法从代码访问区域: 导入的TheRegionManager为null,我必须这样做

         TheRegionManager = ServiceLocator.Current.GetInstance<IRegionManager>(); wich returns a region manager that contain only the 2 regions from the Shell window but not the 2 regions from this other window that I want.

        I'm sure that I'm missing something. Maybe I need to add some code to the bootstrapper. Or I should add this other window as a module in the catalog? Why does it work fine in Shell window and not in this other window?

1 个答案:

答案 0 :(得分:0)

转到窗口的代码隐藏:

    protected override void OnInitialized(System.EventArgs e)
    {
        base.OnInitialized(e);

        var regionManager = ServiceLocator.Current.GetService(typeof(IRegionManager)) as IRegionManager;
        RegionManager.SetRegionManager(/*content control for region manager*/, regionManager);
    }

现在处于您的查看模式:

TheRegionManager.RegisterViewWithRegion(/*region name*/, typeof(/*view type*/));