RequestNavigate in TabControl region only activates the existing view

Topics: Prism v4 - WPF 4
Apr 2, 2011 at 3:47 PM

Hello,

I have a region adapting a TabControl. I want to open multiple new views as tabs via Region.RequestNavigate. The first time it creates a new view and activates the new tab. But the following times it only activates the existing tab. My View and my ViewModel do NOT implement INavigationAware.

When the ViewModel implements INavigationAware and IsNavigationTarget returns false, then additional views and tabs are created as expected.

Is this the correct behavior? This means my ViewModels used in Tab-Regions always must implement INavigationAware just to be able to be showed multiple times.

 

Erik

IsNavigationTarget
Apr 5, 2011 at 12:50 AM

Hi Erik,

The behavior you are experiencing is the default behavior of Prism´s Navigation Service. When you call the RequestNavigate method, the container will try to resolve an instance of the view you’ve specified through the Uri. If you have exported your view through MEF’s Export attribute without specifying a PartCreationPolicy, it will be exported as a shared instance by default, therefore making the RequestNavigate method retrieve the same instance of the view on each call. You could try specifying the PartCreationPolicy to CreationPolicy.NonShared.

If that doesn’t fix your undesired behavior, it would be helpful if you could provide us with additional information regarding your application, or a repro sample if possible.

You might find the following blog post by Karl Shifflet useful to learn more about the region navigation mechanisms:

I hope you find this information useful.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic


Apr 6, 2011 at 3:38 PM

Hi Miguel,

thanks for your hints, but I'm using the Unity container not MEF, which by default creates everytime a new instance.

I took a closer look in the prism code and found the RegionNavigationContentLoader.LoadContent method. Here the reuse-candidates are checked for the INavigationAware interface. The last step is checking the DataContext and if it does NOT implement the INavigationAware interface the view is reused. And this is in my opinion a bit strange. The view is reused because it does not know anything about the navigation operation, mmmh.

So my workaround is to implement always the INavigationAware interface with IsNavigationTarget returning false.

 

Erik