Region within a view that lives in itemscontrol region?

Topics: Prism v4 - Silverlight 4
Apr 3, 2011 at 7:56 PM
Edited Apr 3, 2011 at 8:01 PM

Hi,

 

I have an interesting situation and kind of stuck for now.

I have a ToolbarView that I would like to reuse within my application. Therefore I would have three ViewModels that would use the same view.  I thought I could create a base class or interface for the View's ctor in this case.

Now I am actually already using an ItemsControl that is a region within my Shell:

<sdk:TabControl regions:RegionManager.RegionName="MainRegion"/>    
            

Each tab on this region would contain a ContactView that takes a ContactViewModel injected. So far so good.

Now I need to have a Region defined within the ContactView to register a ToolbarView in there and inject my ToolbarCallViewModel in there. A ToolbarView outside my ContactView for example would get a plain ToolbarViewModel injected. etc

I have created a contentcontrol within the Contact View in order to do the plan above:

<ContentControl regions:RegionManager.RegionName="ToolbarCallRegion"/>    
               

Now within my adding a ContactView to the ItemsControl (mainRegion) mechanism I do the following:

// Add the View in a new Tab
var contactViewModel = _container.Resolve<ContactViewModel>();
contactViewModel.Initialize(contact);
var view = new ContactView(contactViewModel);
 
var toolbarCallViewModel = _container.Resolve<ToolbarCallViewModel>();
var viewCallToolbar = new ToolbarView(toolbarCallViewModel);
                
_regionManager.Regions[Constants.MainRegion].Add(view, contact.ContactID.ToString());
_regionManager.Regions[Constants.ToolbarCallRegion].Add(viewCallToolbar, contact.ContactID + "callToolbar");

Now the last line works only the first time I am adding a view. Since its only a ContentControl, this approach seems wrong to me. How would it even know that this Region is meant to be unique within that ContactView?

I hope you guys know what I mean,
Thanks for help,

Houman 

Developer
Apr 4, 2011 at 5:11 PM

Hi Houman,

From my understanding of your scenario, you could benefit from using scoped regions. Since only one region with the same name can exist in a RegionManager, if a view that contains a region is added more than once, a scoped region manager should be created in order for the new regions created to be placed in separate region managers. The IRegion.Add method has an optional boolean parameter that specifies if you wish to create a scoped region manager, and returns that region manager.

Therefore, your last lines could be modified to something like this:


// Add the View in a new Tab
var contactViewModel = _container.Resolve<ContactViewModel>();
contactViewModel.Initialize(contact);
var view = new ContactView(contactViewModel);
 
var toolbarCallViewModel = _container.Resolve<ToolbarCallViewModel>();
var viewCallToolbar = new ToolbarView(toolbarCallViewModel);
                
var scopedRegionManager = _regionManager.Regions[Constants.MainRegion].Add(view, contact.ContactID.ToString(), true);
scopedRegionManager.Regions[Constants.ToolbarCallRegion].Add(viewCallToolbar, contact.ContactID + "callToolbar");

You can read more about scoped regions in the following chapter from the Prism MSDN documentation:

Chapter 7: Composing the User Interface

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Apr 5, 2011 at 8:40 AM

Thank you so much Guido. 

Your code example made it very clear how this works.

Have a good day,

Houman