Multiple view in the same region (TabControl/RadDocking)

Topics: Prism v4 - Silverlight 4, Prism v4 - WPF 4
Oct 28, 2010 at 1:34 PM

Hello,

I have a RadDocking control from Telerik. I defined a region on document host. It works very well until I try to add twice the same view.

I tried to add a different name when adding the view to the IRegion, but it didn't work. I always gave the "View already exists in region." exception.

 

How could I solve this? Region behavior? etc?

 

 

Thanks,

Adrien Pellegrini.

Oct 29, 2010 at 7:42 PM
Edited Oct 29, 2010 at 7:45 PM

Hi Adrien,

In Prism you can add two different instances of the same view in the same region. But it is not possible to add the same instance twice by design. As you are experiencing it throws a System.InvalidOperationException.

If you resolve this view using your container and you do not want to add the same "instance" twice, you could check the way that you are registering your view. Please take into account that if you are using Unity and also use a ContainerControlledLifetimeManager, it will retrieve you a Singleton of your view. The same occurs if you are using MEF and indicates a CreationPolicy.Shared when exporting your view.

Hope this helps.

Fernando Antivero

http://blogs.southworks.net/fantivero

Oct 29, 2010 at 7:54 PM

Thanks for your response.

I don't use Unity. I use only MEF (I'm trying for now ...). Views aren't registered in the container. When a module is initialized, the constructor get an instance of a view model and the view model gets an instance of the view.After in the initialize method of the "IModule" module, I add the view to the region by view injection.

I read that, what I want to do, will be easier using view discovery. Also, I'm questionning myselft about registering viewmodels in the container and try to add them in the region. Thanks to that I'll be able to have two identical view but with two different data context.

I'm not sure I'm on the right way, am I?

 

My goal is to have the "same" view (defined in a module) added twice in a document host (or TabControl, it should be identical regarding the way to do it). By same view I mean that the usercontrol is the same but it is two separate instance and both have their own view model.

Oct 29, 2010 at 8:27 PM

Hi Adrien,

If you need two instances of the same view in the same region, you must use View Injection since it is necessary to indicate a view name. Otherwise, you will receive an exception.

Regarding to your particular situation, you could check the way that your view model is creating your view, since it seems that retrieve the "same" instance of the view. And this is why you are experiencing a System.InvalidOperationException when injecting the second view in your region.

In conclusion: In Prism you can add the same view twice in a region, but they must be different instances. As you use MEF, I assume that you are importing your views, so please check that the Export attribute of the view does not indicate CreationPolicy.Shared, since it will retrieve the same instance when importing.

If you continue experiencing this situation in your scenario, could you please share a repro sample? For example you could use Windows Live SkyDrive.

Hope this helps.

Fernando Antivero

http://blogs.southworks.net/fantivero

 

 

Oct 30, 2010 at 12:15 PM

Well, I feel stupid ... It was very simple in fact.

Thanks.

Nov 1, 2010 at 6:06 PM

Hi Adrien,

I am glad that you found a solution in your scenario. Please let me know if you have any other questions.

Fernando Antivero

http://blogs.southworks.net/fantivero