2
votes

I am using Prism, MEF (MEF Bootstrapper), and WPF. I am new to these and I have a very simple app that I'm trying to get to work.

I have two regions in the main window (shell) which work fine. I can see them in the window at runtime and I can access them through the TheRegionManager.Regions["regionName"] from code inside the Shell window and in other windows. For this I use :

[Import]
public IRegionManager TheRegionManager {private get; set;}

Now I want to do the same thing for another window in the Application. I have two regions in it and I configure them from XAML and I do:

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

To register the views like in the other 2 regions from Shell. This sort of works because I can see the views loaded in the regions at runtime. The problem is I cannot access the regions from code: The imported TheRegionManager is null, I have to do

         TheRegionManager = ServiceLocator.Current.GetInstance<IRegionManager>();

which 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
I solved my problem eventually. After getting the RegionManager with service locator I had to do RegionManager.SetRegionManager(view,..) to set the region manager to the view.Paul Rusu
Is it solved? If so, answer or close your question.PVitt

1 Answers

0
votes

Go to codebehind of your window and:

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

now in your view mode:

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