View Multiple

Topics: Prism v4 - WPF 4
Aug 17, 2011 at 5:31 AM

How I can show one View on many region with different names?

Developer
Aug 17, 2011 at 5:58 PM

Hi,

Based on my understanding of your scenario, you want to show a single view on multiple regions. In order to do this you can register the view in each region like this:

this.regionManager.RegisterViewWithRegion("Region1",typeof(MyView));
this.regionManager.RegisterViewWithRegion("Region2",typeof(MyView));

[...]

 

Or, if you want to show the view programmatically, like this:

var firstInstanceOfMyView = this.serviceLocator.GetInstance(typeof(MyView));

IRegion region1 = this.regionManager.Regions["Region1"];
region1.Add(firstInstanceOfMyView);
region1.Activate(firstInstanceOfMyView);

var
secondInstanceOfMyView =  this.serviceLocator.GetInstance(typeof(MyView));

IRegion region2 = this.regionManager.Regions["Region2"]; region2.Add(secondInstanceOfMyView); region2.Activate(secondInstanceOfMyView); [...]

 

Note that you have to add a different instance of the view in each region (as shown above) because the same instance cannot be added twice in the same visual tree. In case you are using Unity, its default behavior is to create a new instance of the view each time it is resolved (unless you have used the RegisterInstance method or the RegisterType method passing a ContainerControlledLifetimeManager as a parameter), however, if you are using MEF you will have to decorate your view with the PartCreationPolicy attribute specifying the NonShared value. You can read more about this in the following chapter from the Prism MSDN documentation:

You can find more information about how to show a view in a region here:

Please, let us know if we misunderstood your scenario.

I hope you find this useful,

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

Aug 18, 2011 at 5:30 AM

Thanks.

This, is work?

var firstInstanceOfMyView = this.serviceLocator.GetInstance(typeof(MyView));

IRegion region1 = this.regionManager.Regions["Region1"];
region1.Add(firstInstanceOfMyView);
region1.Activate(firstInstanceOfMyView);

IRegion region2 = this.regionManager.Regions["Region2"];
region2.Add(firstInstanceOfMyView);
region2.Activate(firstInstanceOfMyView);

[...]

Aug 18, 2011 at 7:57 AM

Sorry, it does not work, probably.

Aug 18, 2011 at 8:58 AM

I understand that there is no general way to add the same View in different Regions, belonging to the same RegionManager also different RegionManager. Ie in any case can not show a single instance representation in several regions, right?

Developer
Aug 18, 2011 at 6:10 PM
Edited Aug 18, 2011 at 8:47 PM

Hi,

The reason why you cannot add the same instance of a view more than one time is because the WPF's Visual Tree does not allow it. You can find more information about the Visual Tree here.

However, as a possible approach to do this, you could add different instances of the view in each region and use the same view model for all the views.

In this case, as a view obtains all the data from the view model, all the instances of that view will show the same content and will react to changes made in the view model's properties.

If you wish to implement this approach and you are injecting the view model in the view, you will have to export the view model as a singleton instance:

  • If you are using Unity you can use the RegisterInstance method or the RegisterType method passing a ContainerControlledLifetimeManager as a parameter.
  • If you are using MEF you can decorate your view with the PartCreationPolicy attribute specifying the Shared value.

Also, please note that the view should not be exported as a singleton.

I hope you find this useful,

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

Aug 19, 2011 at 2:24 AM

Thanks.

I understood everything.