Redirecting Navigation in Windows Phone 8

I am working on an app for Windows Phone 8 that uses some custom navigation. I made a class that inherits from UriMapperBase and set it into the RootFrame during application startup. In this mapper I can handle custom Protocols and redirect them to pages in my application.

I also use this mechanism to forward the user to a page to ask them to allow or deny the app rights to use the location services the first time its loaded up. This resulted in a strange case where I was actually redirecting the user to a page other than what they were actually navigating to. When I then attempted to navigate them to the correct page it ended up being a no-op since the current page was at the top of the navigation stack as the uri I was attempting to navigate to. In other platforms the NavigationService has a Refresh method you can call to resolve such problems but in Windows Phone 8 it is mysteriously missing. So after wasting a couple hours I managed to find a way to resolve this cunundrum.

Simply create a page in your application called Refresh.xaml and Navigate to this page. Something like this:

private void OnAllowClick(object sender, RoutedEventArgs e)
{
  LocationService.AllowLocationServices = true;
  this.NavigationService.Navigate(new Uri("/Views/Refresh.xaml", UriKind.Relative));
}

Then in Refresh.xaml you simply pop the last item off the navigation stack and navigate back to it:

public partial class Refresh : PhoneApplicationPage
{
  public Refresh()
  {
    InitializeComponent();
  }
  protected override void OnNavigatedTo(NavigationEventArgs e)
  {
    var j = this.NavigationService.RemoveBackEntry();
    this.Dispatcher.BeginInvoke(() => App.RootFrame.Navigate(j.Source));
    base.OnNavigatedTo(e);
  }
  
  protected override void OnNavigatedFrom(NavigationEventArgs e)
  {
    this.NavigationService.RemoveBackEntry();
    base.OnNavigatedTo(e);
  }
}

This will refresh to the current uri and preserve the navigation stack correctly.