Update Prism Region Manager with Regions Created Dynamically

Topics: Prism v4 - WPF 4
Jul 16, 2012 at 8:09 PM

I am trying to update the Region Manager with the regions from my nested views.

Here is my current project structure.

Shell.xaml has 4 tabitems with each tabitem being a project module.

Now TabItem1 has a content control that loads the view1 correctly. There is a content control within this View1 that loads View2. View2 has another content control which loads View3.

Now in my View3 i have a Tab control in which i have to add objects dynamically using the controller but it does not recognise the regions from View1 onwards. How can i force the Region Manager to read all these regions?

Developer
Jul 16, 2012 at 9:42 PM

Hi,

It would be helpful, if you could provide us with more information about your scenario, so that we can help you further with this. For example, how you are defining the regions in your views, if you are using scoped regions, how your views are being added to the regions, etc. As there may be different reasons for these behavior.

For example, one possible cause could be if your are defining your regions inside a DataTemplate, for more information about this you could check the following blog post:

Or if you are using scoped regions, note that each view will have its own RegionManager and its regions will be registered with that RegionManager rather than the parent RegionManager.

Regards,

Agustin Adami
http://blogs.southworks.net/aadami

Jul 17, 2012 at 2:06 PM
Edited Jul 17, 2012 at 7:44 PM

Aadami,


I am not using data templates.
Tabitem1 in my shell.xaml is opening a View1 which is a tab control and has a region that points to view2. Multiple Views of view2 are being added to the TabControl in View1 using a Controller class.

In the Controller class it adds the view2 to the Tab control in view1.

The code in my View1 controller classs is as follows:

  View2  view2 =  new   View2  ();

view2.View2ViewModel = _serviceLocator.GetInstance< View2ViewModel>();

view2.PatientViewModel.Model = p;

_regionManager.Regions[RegionNames .View2TabsRegion].Add(view2, p.pGUID.ToString(), true);

_regionManager.Regions[RegionNames  . View2TabsRegion].Activate(view2);

 

 In View2 in one of the Wpf grid I have another content control with regions that opens up view3. View3 has a tab control in which one of the tab items region is view4.

View3 calls another controller class with the following signature:

[ImportingConstructor]

 public View3Controller(IRegionManager regionManager, IServiceLocator serviceLocator, IEventAggregator

eventAggregator)

{

_eventAggregator = eventAggregator;

_serviceLocator = serviceLocator;

 

_regionManager = regionManager;

 

_eventAggregator.GetEvent<NewForm>().Subscribe(this.AddNewForm, ThreadOption.UIThread, true);

 

 

}

This is how i am Adding the RegistredViews

 

 

 

 

 

 

 

 

 

private void

AddRegisteredViews()

{

 

 

if (this.Region != null

)

{

 

 

foreach (var viewEntry in this

.RegisteredViews)

{

 

 

if (viewEntry.Metadata.RegionName == this

.Region.Name)

{

 

 

var

view = viewEntry.Value;

 

 

if (!this

.Region.Views.Contains(view))

{

 

 

this

.Region.Add(view);

}

}

}

}

}

 

 

 

 

So when i check teh _regionManager.regions it does not have the regions defined in View2 and all its child views.

Developer
Jul 17, 2012 at 6:46 PM

Hi,

So far, we were unable to reproduce the behavior you are mentioning with the information you provided.

It would be helpful if you could isolate this behavior in a repro-sample application portraying this so that we can find the cause behind the problem you are experiencing and help you further with it.

Regards,

Damian Cherubini
http://blogs.southworks.net/dcherubini

Jul 17, 2012 at 7:45 PM

DCherubini,

I modified my code. Please have a look and let me know if you need any more information.

 

DCherubini wrote:

Hi,

So far, we were unable to reproduce the behavior you are mentioning with the information you provided.

It would be helpful if you could isolate this behavior in a repro-sample application portraying this so that we can find the cause behind the problem you are experiencing and help you further with it.

Regards,

Damian Cherubini
http://blogs.southworks.net/dcherubini

 

Developer
Jul 17, 2012 at 10:01 PM

Hi,

After checking the code snippets you provided, we believe that the cause of your problem is that you are adding the View2 with a scoped region manager, this can be seen in the following line you provided, in which the createRegionManagerScope parameter of the Region.Add method is being set to true:

_regionManager.Regions[RegionNames .View2TabsRegion].Add(view2, p.pGUID.ToString(), true);

Take into account that when using scoped regions managers, each view will have its own RegionManager and its regions will be registered within that RegionManager rather than the parent RegionManager, this is why if you retrieve the parent RegionManager this regions won't be available.

Hence, instead of using the parent RegionManager to access the inner regions you should retain the new RegionManager returned by the Add method, which will allow you to access to the local scope.

For more information about scoped RegionManager, I believe you could check the following section of the Prism documentation:

Also, you could check the sample in the UI Composition Lab (exercise 3) of the Prism Training Kit which uses scoped regions; note that this lab is using Unity and Silverlight, but the concepts explained there may be still useful.

Regards,

Agustin Adami
http://blogs.southworks.net/aadami

Jul 17, 2012 at 10:42 PM
Edited Jul 19, 2012 at 9:23 PM

Aadmi,

You are awesome. That was it. I had set the createRegionManagerScope to true and hence it was not working but now my region manager updates correctly.

One more question. "In View2 in one of the Wpf grid I have another content control with regions that opens up view3. View3 has a tab control in which one of the tab items region is view4."

So now when i try to add mutiple Tabs of my View2 it errors saying View3 has already been added. Is there a way in XAML that i can add my View3 with a unique id ?

 

Thank You so much.