NavigationService.CanGoBack not up to date in INavigationAware OnNavigatedTo method

Topics: Prism v4 - WPF 4
Aug 7, 2012 at 10:46 AM

I want to bind to the NavigationService.CanGoBack method in my view, so I know whether a user can navigate backwards. If they can go back it shows a Back Button, otherwise it is the icon of the screen they are on.

So I create a property wrapper like so:

public bool CanGoBack {           

get  {  return _navigationService != null ? _navigationService.Journal.CanGoBack : false;  }       

}

The problem is I need the view to be aware when this changes. So I thought in a ViewModels OnNavigatedTo method to RaisePropertyChanged on this. But the issue is the journal doesn't appear to be up to date with the currententry at this point, it is only after this it is up to date. So the problem occurs when I navigate from View1 to View2, it is correct, but when I go back to View1 it is incorrect and thinks it can go back. How could I get round this?

Here is my OnNavigatedTo method:

public override void OnNavigatedTo(NavigationContext navigationContext)        {            base.OnNavigatedTo(navigationContext);           

_navigationService = navigationContext.NavigationService;

RaisePropertyChanged("CanGoBack");

}

Developer
Aug 7, 2012 at 2:52 PM
Edited Aug 7, 2012 at 2:54 PM

Hi,

I believe, you could try subscribing to the Navigated event defined in the region navigation service, this way you will be notified when navigation within a region is completed. Then in its event handler you could notify the UI that it has to reevaluate the CanGoBack property for example by calling the RaisePropertyChanged method as you mentioned. I believe that using this approach, you shouldn't experience the timing issue you mentioned.

Please let us know if this helps to solve your problem,

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

Aug 8, 2012 at 7:34 AM

Yep that's good thanks