Adding and Swapping Multiple Views to a Region

Topics: Prism v2 - Silverlight 2
Jul 20, 2009 at 1:34 PM

Hi,

I am new to Prism and I have a very basic question.

Is there a way to add Multiple Views to a Region maintaining just one of them visible and, when
necessary, swap the contents of the region to another one.

In the first time a view is selected it will created it, the WCF services called, the results
will be stored in the ViewModel and bound to the View Controls.

What I want is to create the views as they area displayed for the first time and then be able to
swap the views. The created views must preserve their contents and visual aspects even when they
are not currently visible.

Thank you in advance

Jul 20, 2009 at 7:06 PM

you could just use a contentcontrol as region and only one of the views will be active at anytime. if you add the views with names then all you have to do is activate one of the view passing the name of the view

Jul 20, 2009 at 7:22 PM

Hi,

I presume you are talking about registering all the views in the Region, like this:

  regionManager.RegisterViewWithRegion("ClientAreaRegion", typeof(View1));
  regionManager.RegisterViewWithRegion("ClientAreaRegion", typeof(View2));
  .
  .
  .
  regionManager.RegisterViewWithRegion("ClientAreaRegion", typeof(Viewn));

And then activate a view like this:

  region.Activate(view);

There is still a problem. I need to strech the region all over the screen, stretching it
horizontally and vertically. The ContentControl Region Adapter does not do this. I had
to create a ContentPresenterRegionAdapter. With a ContentPresenter I can make the
Region and consequentelly the views displayed stretch in both directions.
 
I beleive that the registration and activation will work with my custom Region Adapter.

I will check it out and as soon as I can I will let you know the result.

Thank you very much

Jul 20, 2009 at 7:32 PM

Hi,

Maybe the association of views to the Region could be:

 IRegion mainRegion = regionManager.Regions["ClientAreaRegion"];

        mainRegion.Add(View1);
        mainRegion.Add(View2);
 ...
        mainRegion.Add(Viewn);

and not the using RegisterViewWithRegion.

Jul 20, 2009 at 7:47 PM

Hi

This scenario is perfectly supported by Prism. You should use a ContentControl as a region, which allows you to only have one visible region at a time, and use the Activate method of the region. The following blog post provides more information about the different regions available out-of-the-box in prism and their behavior:

Please let me know if this helps.

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

Jul 20, 2009 at 8:01 PM

Hi,

I am outside office and I cannot test it right now. But in a few hours I will check it out.

The only difference is that I am using a custom ContentPresenterRegionAdapter that I have created
that, like ContentControl, accepts only one View at a time but it can stretch the region correctly.

I presume that I must use the SingleActiveRegion so that my views can be swapped.

Thanks to all of you. I will let you know the result very soon.

Jul 20, 2009 at 8:54 PM

Hi,

A SingleActiveRegion inherits from Region. How can I make my regions created with my custom
ContentPresenterRegionAdapter a SingleActiveRegion, like the Regions created as a
ContentControl ???

Thanks in advance

Jul 20, 2009 at 9:00 PM

Hi,

I found it. Just clone the code of the classe ContentControlRegionAdapter in
Microsoft.Practices.Composite.Presentation.Regions.

The CreateRegion method returns a SingleActiveRegion.

Aug 29, 2009 at 10:17 PM

I have decided to see if I can solve my similar problem using the techniques in Damian Schenkelman's post. In the example given, all the views are loaded and shown at startup.

In my case, I really only want to see one view at starup (in the future, the control may need to contain >1 visible views) - how do I switch off the display of, say, Views 2 and 3?

Simple code like

this.regionManager.Regions["TabRegion"].Hide("View2");

always fails with the exception saying that "TabRegion" is unknown. Clearly it is a question of where, in the event sequence, I put this code. I thought the Window Loaded event would do, but, same problem.

Any suggestions?

 

 

Aug 29, 2009 at 10:45 PM

Ok - "reply to self"

After a bit of slog, I have got something working:

protected override void InitializeModules()
        {
          base.InitializeModules();
          shell.SetupViewVisibility();
        }

where SetupViewVisibility() does the required show/hide at app start.  All to do with timing wrt when regions are known about  - can anyone confirm that this is a good way t odo this, or point me to details on such timings matters.

Hope of use to someone else!

 

 

Feb 24, 2010 at 4:51 PM
Edited Feb 24, 2010 at 4:56 PM

Hi All,

I am able to add couple of views to ContentControl using Dependency Injection. Based on certain Action (Button Click) I need to Activate Views in Region (Content Control).

I am Bit confused here ?

To perform Activation on Shell (Shell.Xaml.Cs) by subscribing Event (OR)

Can I perform Activation in View Models itself.

Any Suggestions?

Feb 26, 2010 at 6:07 PM

Hi,

The following threads deal with this same scenario (specifically the parts that mention Erwin’s post) should be useful for you:

Please let me know if this helps.

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