IsnavigationTarget returns True but does not activate Tab Item

Topics: Prism v4 - WPF 4
Nov 14, 2012 at 5:29 PM

Hi, 

Problem: I open a new tab on some button click. It opens first time. but if I click that button again, I expect already open tab to be activated. but it doesn't happen.

  public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            // For now we always open a new tab.
            return navigationContext.NavigationService.Journal.CurrentEntry.Uri.ToString().ToLowerInvariant() == navigationContext.Uri.ToString().ToLowerInvariant();
        }

above code returns true. Not sure what am I missing. can someone please shed some light?

 

Developer
Nov 14, 2012 at 6:53 PM

Hi,

So far, with the information provided, we are unable to find the reason behind the behavior you are describing.

It could be useful if you could provide us with more information about your scenario, like how you are navigating to the view, etc, so that we can help you find the cause behind your problem.

Regards,

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

Nov 14, 2012 at 8:49 PM

Sure Damian,

This was working before. and I'm not sure what I changed. but anyway, here's little more detail. 

we are constructing Tab items using view injection. inside every tab item, there're multiple views added using requestNavigate(all internal regions)

Tab A

 - left region

- right region

- tool bar region

on click of a button in TabA's right region, I navigate to Tab B with similar internal regions (slight variation inside due to business logic)

if Tab B is open, and I click on a TabA's button, it should just go to already existing View(Tab B). this does'nt happen anymore. when I click the button, the focus remains on Tab A. the focus does not change to Tab B at all. (If I close Tab B, click button again, Tab B opens with focus on it)

Hopefully I'm making sense?

 

Kavya

Developer
Nov 15, 2012 at 1:29 PM

Hi Kavya,

As as starting point, I believe you could subscribe to the NavigationFailed event in the corresponding NavigationService instance of that region, too see if an exception is being thrown while performing the navigation. This information could be helpful to analyze the cause of this behavior. For more information about this, you could check the following related blog post:

On the other hand, so far the only scenario I could think of, that could cause a similar behavior to the one you mentioned, could be if both your view and the corresponding view model implements the INavigationAware interface, in which case the value of the IsNavigationTarget=true returned in your view could be ignored if your view model returns a different value. This behavior has been reported in the following work item:


I hope this helps to find the cause of the problem,

Agustin Adami
http://blogs.southworks.net/aadami

Nov 19, 2012 at 3:38 PM

Hi,

there seems to be an issue where IsNavigationTarget is not called if the name of the view navigated to is different from the type name of the view class.

I.e., when your view is called "CustomersList", but is exported as "CustomersListView", the method won't be called when navigating to "CustomersListView".

I'm not sure if this is supposed to be that way, but we just had the same problem in a Prism training class.

Best regards,

Fabian

Nov 26, 2012 at 3:04 PM

thank you both. I was on vacation so couldn't respond.

if you can look at this, would be really helpful. 

I have similar approach to the code attached in this link

http://compositewpf.codeplex.com/discussions/394860

Kavya_TabApproach1 

Of course, I've later changed to view injection. but above problem still exists in this code. I have a tab item view in which i have multiple views. 

Click on "Goto datapool". a tab opens.

click on same row again, already opened tab doesn't get activated.  I verified Fabian's suggestion. I'm exporting same as view name. Export[""

can you please help? I'll also try the NavigationFailed event raising

Kavya

Nov 26, 2012 at 3:53 PM

I managed to grab the error. it says, "RIghtRegion" doesn't exist. (When I click second time on the same row in the first tab). 

on OnNavigatedFrom, regions are removed. however, aren't thy created later ? 

        void NavigationService_NavigationFailed(object sender, RegionNavigationFailedEventArgs e)
        {
            
        }

      
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
          
            return navigationContext.NavigationService.Journal.CurrentEntry.Uri.ToString().ToLowerInvariant() == navigationContext.Uri.ToString().ToLowerInvariant();
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            regionManager.Regions.Remove("LeftRegion");
            regionManager.Regions.Remove("RightRegion");
            
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            this.innerNavigationContext = navigationContext;
            navigationContext.NavigationService.Navigated += NavigationService_Navigated;
            regionManager.Regions["MainRegion"].NavigationService.NavigationFailed += new EventHandler<RegionNavigationFailedEventArgs>(NavigationService_NavigationFailed);
        }

        void NavigationService_Navigated(object sender, RegionNavigationEventArgs e)
        {
            this.innerNavigationContext.NavigationService.Navigated -= NavigationService_Navigated;

            if (!navigatingInternally)
            {
                navigatingInternally = true;

                RegionManager.UpdateRegions();

                this.PerformInternalNavigation(this.innerNavigationContext);
                this.innerNavigationContext = null;

                navigatingInternally = false;
            }
        }

        private void PerformInternalNavigation(NavigationContext navigationContext)
        {
            string rightRegionViewName = navigationContext.Parameters["RightRegion"];
            string leftRegionViewName = navigationContext.Parameters["LeftRegion"];

            regionManager.Regions["RightRegion"].RequestNavigate(new Uri(rightRegionViewName + navigationContext.Parameters.ToString(), UriKind.Relative), CheckRightRegionHeader);
           regionManager.Regions["LeftRegion"].RequestNavigate(new Uri(leftRegionViewName + navigationContext.Parameters.ToString(), UriKind.Relative));
          
           
        }

please help
Nov 26, 2012 at 3:55 PM

sorry. forgot to add the line

 regionManager.Regions["MainRegion"].NavigationService.NavigationFailed += new EventHandler<RegionNavigationFailedEventArgs>(NavigationService_NavigationFailed);

(I couldn't edit the above post for some browser issue)

private void PerformInternalNavigation(NavigationContext navigationContext)
        {
            string rightRegionViewName = navigationContext.Parameters["RightRegion"];
            string leftRegionViewName = navigationContext.Parameters["LeftRegion"];

            regionManager.Regions["RightRegion"].RequestNavigate(new Uri(rightRegionViewName + navigationContext.Parameters.ToString(), UriKind.Relative), CheckRightRegionHeader);
           regionManager.Regions["LeftRegion"].RequestNavigate(new Uri(leftRegionViewName + navigationContext.Parameters.ToString(), UriKind.Relative));
          
           regionManager.Regions["MainRegion"].NavigationService.NavigationFailed += new EventHandler<RegionNavigationFailedEventArgs>(NavigationService_NavigationFailed);
        }

Nov 26, 2012 at 5:30 PM

Thank you for pointing to me the right direction. I resolved it by doing every view through view injection in scopedregion manager than through navigation. 

the problem was, in above code isNavigatedFrom, the regions were removed. 

so for strange reasons, when I navigated second time, it kept saying, "Rigth region is not available" in function

PerformInternalNavigation

. (i used navigationFailed event)

 

Many thanks.

Kavya