简体   繁体   中英

How to properly navigate backstack in Windows Phone 8.1 universal store app

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM