1

I am working on an app for WP 8.1, it uses Windows Runtime and not Silverlight. Because of this I have no access to the NavigationService class.

I have tried to manually implement a Back Stack by pushing the current page type to the stack in the OnNavigatedTo() method with no luck.

All I need to do is find a way to manage navigation history so that when a user hits the hardware back button they are taken to the last page in the app they were on, until the user is on the first page where hitting back would exit the application.

Can someone point me in the right direction here?

Bryan
  • 3,199
  • 3
  • 16
  • 24
  • possible duplicate of [Windows Phone 8.1 Universal App terminates on navigating back from second page?](http://stackoverflow.com/questions/24335925/windows-phone-8-1-universal-app-terminates-on-navigating-back-from-second-page) – Igor Ralic Feb 04 '15 at 14:12

2 Answers2

1

You can access the backstack via the BackStack property of your navigation frame. To do that you can simply override the GoBack command inside your NavigationHelper class (you can find it inside the Common folder). There you can simply check the BackStackDepth: if it's 0 then you can terminate the current app, as the default behaviour would suspend it without closing it :)

Something like (in the NavigationHelper class):

public virtual void GoBack()
{
    if (this.Frame != null)
    {
        if (this.Frame.CanGoBack) this.Frame.GoBack();
        else App.Current.Exit();
    }
}
Sergio0694
  • 4,447
  • 3
  • 31
  • 58
  • I didn't have a common folder because I started out with a "Blank App". I added a Basic Page and it created the Common folder for me with the navigation helper. Now everything is working as desired. Thanks! – Bryan Feb 04 '15 at 14:23
0

The Frame class now holds the Navigation methods (BackStack, GoBack, Navigate, etc.)

Get it via (in a Pages Codebehind):

((Frame)Parent).BackStack;

Or somewhere else via:

((Frame)Window.Current.Content).BackStack;
Kai Brummund
  • 3,538
  • 3
  • 23
  • 33