8
votes

Here's the scenario for my page navigation:

MainPage (MP) <==> Locations (L) <==> AddLocation (AL)

I don't want the user to ever go to AddLocation when hitting the back button on the phone.

If they are on MP and they hit back, they should exit the app.

If they went MP->L and hit back, they should go to MainPage.

If they went MP->L->AL and hit back, they should go to Locations.

If they went MP->L->AL->L and hit back, they should go to MainPage and not back to AddLocations.

Right not, I have the standard nav helpers in the Common folder and then I've added this code to the Locations page to make this happen:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    this.navigationHelper.OnNavigatedTo(e);
    var bs = Frame.BackStack.Where(b => b.SourcePageType.Name == "MainPage").FirstOrDefault();
    if (bs!= null)
    {
        Frame.BackStack.Clear();
        Frame.BackStack.Add(bs);
    }
}

This seems to be a terrible hack to me and I'm sure there some supported/designed way to do this that I don't know about. I'm very new to WinRT and Xaml.

1

1 Answers

4
votes

Your problem is went MP->L->AL->L and hit back, they should go to MainPage and not back to AddLocations isn't it?

The solution is: when you finish adding location, you should go to Locations Page from AddLocation Page.

But you should not use Frame.Navigate(typeof(LocationPage)); to do this.

You should use Frame.GoBack();, So AddLocation Page will be removed from BackStack auto.