The region does not contain the specified view

Topics: Prism v2 - WPF 3.5
Apr 19, 2010 at 5:03 PM

Hi

 

I have 2 regions setup in my shell ToolBarRegion(ItemsControl) and DetailsRegion(ContentControl).  I load 3 modules in the bootstrapper

Companies: This module will display a list of companies

 

When this module get initialized it gets added to the DetailsRegion.

IRegionManager regionManager;

Public Companies(IRegionManager _regionManager)

{

regionManager = _regionManager;

}

public void Initialize()

{

CompanyDetails cd = new CompanyDetails();

IRegion region = regionManager.Regions["DetailsRegion"];

region.Add(cd);

}

Users: This module shows a list of users and it's initialized like the code above

MainToolBar: When initialized gets added to the ToolBarRegion and then activated - like above code but also calls region.Activate(viewname).

This module will display buttons which will have their tag property bound to the name of the view to be displayed.  These views are in the other modules(Companies,Users) that get loaded in the bootstrapper.

I have a delegate command in my MainToolBarViewModel called ShowView.  This is set as the command for the button.

It does the following

public void ShowView(object param)

{

this.regionManager.Regions["DetailsRegion"].Activate(param)

}

It breaks on the above line with the following error "The region does not contain the specified view Parameter name:view"

The param object is showing the correct name for the view i want to show, also if i drill into the Regions and expand the Results view it shows 2 entries 1 for the ToolBarRegion and 1 for the DetailsRegion.

If i expand the DetailsRegion and then expand the Views then expand Results view i can see the 2 views i have loaded into this region.

[0] {Users}

[1] {Companies}

Can anybody shed some light on what i'm doing wrong here.

I've also had a look at the following post http://compositewpf.codeplex.com/Thread/View.aspx?ThreadId=67573 but still same error whichever way i try.

Thanks

Paul

 

 
Apr 19, 2010 at 7:39 PM
Edited Apr 19, 2010 at 7:41 PM

The Activate method takes the view that you want to activate as the parameter. It would appear from your description that you are passing a string to the Activate method.

You should try:

regionManager.Regions["DetailsRegion"].Activate(regionManager.Regions["DetailsRegion"].GetView(param));

 

<edit> But I don't think that will work unless you add the views using a name, region.Add(cd, "CompanyDetails");

Apr 19, 2010 at 7:53 PM

Hi Paul,

The activate method takes an object as a parameter that is supposed to be the View (not the View‘s name). To get the view specifying the name, you can use the GetView(string viewName) method, and then call the activate method.

Something I noted from your implementation is that the MainToolbar buttons know about view names in other modules. This is not recommended, as it highly increases the coupling of your modules, which are supposed to be isolated from each other. The recommended approach is that each module is in charge of handling its own views.

A possible way to decouple the modules could be getting ToolBarRegion filled with views from each of those modules. Then each of the ToolBar items should only be call functionality from its own module.

Please let me know if this helps.

Damian Schenkelman
http://blogs.southworks.net/dschenkelman

Apr 19, 2010 at 9:57 PM

Hi Waday

i tried using the GetView method (GetView(param.ToString()) but get the following error

Value cannot be null.
Parameter name: view

Thanks

Paul

Apr 19, 2010 at 10:04 PM
paul3654 wrote:

Value cannot be null.
Parameter name: view

I would guess that either you did not add the view to the region using a name for the view, or the parameter you are getting passed to the ShowView method is null. See the Region.Add method for adding a view to a region with a name. 

Apr 19, 2010 at 10:22 PM
dschenkelman wrote:

Hi Paul,

The activate method takes an object as a parameter that is supposed to be the View (not the View‘s name). To get the view specifying the name, you can use the GetView(string viewName) method, and then call the activate method.

Something I noted from your implementation is that the MainToolbar buttons know about view names in other modules. This is not recommended, as it highly increases the coupling of your modules, which are supposed to be isolated from each other. The recommended approach is that each module is in charge of handling its own views.

A possible way to decouple the modules could be getting ToolBarRegion filled with views from each of those modules. Then each of the ToolBar items should only be call functionality from its own module.

Please let me know if this helps.

Damian Schenkelman
http://blogs.southworks.net/dschenkelman

 Hi Damian

The MainToolBar buttons are bound to an observablecollection.  This collection is a list of modules that the logged in user has permissions to run.  I have tables in a database for users, roles, modules etc. The table modules has a field that holds the name of the view associated with that module.

My MainToolBar view is a listbox of buttons that are bound to properties of the observablecollection name of button, button image, view to activate etc.

The buttons command is bound to a command in the viewmodel and the command parameter is the name of the view to activate from the observablecollection.

All the above works fine in that all the data is returned from the database as expected i've just hit a wall in trying to activate the views.  In your opinion am i doing this the wrong way?  Is there a better approach in loading modules depending on the role a user is in?

I still would have tought the the GetView method would have worked as i said when i drill into the regionmanager i can see the view listed but the GetView method still returns null.

Thanks

Paul

 

Apr 19, 2010 at 10:27 PM
waday wrote:
paul3654 wrote:

Value cannot be null.
Parameter name: view

I would guess that either you did not add the view to the region using a name for the view, or the parameter you are getting passed to the ShowView method is null. See the Region.Add method for adding a view to a region with a name. 

 Hi Waday

When this module get initialized it gets added to the DetailsRegion.

IRegionManager regionManager;

Public Companies(IRegionManager _regionManager)

{

regionManager = _regionManager;

}

public void Initialize()

{

CompanyDetails cd = new CompanyDetails();

IRegion region = regionManager.Regions["DetailsRegion"];

region.Add(cd);

}

When i check the parameter passed to the ShowView method it is the name of the view that i'm trying to activate.

Thanks

Paul

Apr 19, 2010 at 10:35 PM
Edited Apr 19, 2010 at 10:35 PM
paul3654 wrote:
region.Add(cd);

 

When i check the parameter passed to the ShowView method it is the name of the view that i'm trying to activate.

Paul,

I understand that. You added the view object to the region but trying to access it by name. The type name of the object and the string name that you would refer to it by are not the same thing. They could be not but not necessarily.

To be able to access the view by name you have to add it to the region using a name. as in, region.Add(cd, "CompanyDetails");

 

Apr 19, 2010 at 10:52 PM
waday wrote:
paul3654 wrote:
region.Add(cd);

 

When i check the parameter passed to the ShowView method it is the name of the view that i'm trying to activate.

Paul,

I understand that. You added the view object to the region but trying to access it by name. The type name of the object and the string name that you would refer to it by are not the same thing. They could be not but not necessarily.

To be able to access the view by name you have to add it to the region using a name. as in, region.Add(cd, "CompanyDetails");

 

Ah i get it now, Brilliant Waday that worked a charm. 

Thanks very much i've been staring at this for a while now probably to long :)

Paul

Apr 19, 2010 at 10:56 PM

Glad to help.