views:

829

answers:

1

Hi All,

I have been learning Prism and Silverlight and am now trying to create a simple application but have run into a snag...

What I have is a Shell with 2 ContentControls, "MenuRegion" and "ContentRegion". The way I want it to work is that a user will click on an item in the "MenuRegion" and then the approriate view will be displayed in the ContentRegion, replacing whichever View was previously there (All views are in their own projects, as if being worked on by different people). All of the views to display in the content region will be different.

I do not know how to achieve this using the Prism model (I can do this if I want, but there is going to be lots of code in the View.cs and would like to do this the correct way). All samples I have found out there use a Tab control and load all Modules at once, which isnt what I'm trying to achieve. Is there a particular pattern I should learn or any examples that you know of that could point me in the right direction?

A: 

Give this a try.

Use the RegionManager to add a view to your region:

regionManager.AddToRegion("ContentRegion", new MyViews.View1());

Then when you want to replace that view you can either remove the view:

regionManager.Regions["ContentRegion"].Remove(view);

Or loop through the views in the region and remove them all:

    foreach (var view in regionManager.Regions["ContentRegion"].Views)
    {
        regionManager.Regions["ContentRegion"].Remove(view);
    }

Your view model would be a good place to place this kind of code. Use Unity to inject the region manager in your view model constructor.

DaveB
i'm not sure the last option will work as this is altering the enumeration while looping through it.
bobwah
That is a good point. How could this be done with Prism? Copy the views to you own collection and then enumerate it?
DaveB
I think you can either not remove views once you have registered them with a region and instead Activate or Deactivate views within a region, or add the view with a name (view.Name = "View") and then RegionManager.Regions["Region"].Remove(RegionManager.Regions["Region"].GetView("View"))
bobwah