locked
[UWP][C#]Nested page back button navigates to root page RRS feed

  • Question

  • I have an application that has 3 levels of pages.

    Root Page->List Page->Detail Page

    The root page can only navigate to the detail page by traversing the list page.

    When I click the back button on the detail page I am returned to the Root Page rather than the List Page.

    For the navigation I am using this example from MSDN

    Frame rootFrame = Window.Current.Content as Frame;

    if (rootFrame == null)               

    return;           

    // Navigate back if possible, and if the event has not            

    // already been handled .           

    if (rootFrame.CanGoBack && e.Handled == false)           

    {               

    e.Handled = true;               

    rootFrame.GoBack();           

    }

    I am a bit confused as this suggests to me it should go back up one page not to the root?

    Tim


    • Edited by Tim_D_Jordan Tuesday, August 16, 2016 4:30 AM Update tags
    Monday, August 15, 2016 7:28 PM

Answers

  • Apologies have updated the title.

    This pointed me in the right direction, at somepoint I must have stripped the async from the OnNavigatedTo

    protected override void OnNavigatedTo(NavigationEventArgs e)
    When I added it back

    protected async override void OnNavigatedTo(NavigationEventArgs e)
    It all kicked into life

    • Marked as answer by Tim_D_Jordan Tuesday, August 16, 2016 4:57 AM
    Tuesday, August 16, 2016 4:57 AM

All replies

  • I have an application that has 3 levels of pages.

    Root Page->List Page->Detail Page

    The root page can only navigate to the detail page by traversing the list page.

    When I click the back button on the detail page I am returned to the Root Page rather than the List Page.

    For the navigation I am using this example from MSDN

    Frame rootFrame = Window.Current.Content as Frame;

    if (rootFrame == null)               

    return;           

    // Navigate back if possible, and if the event has not            

    // already been handled .           

    if (rootFrame.CanGoBack && e.Handled == false)           

    {               

    e.Handled = true;               

    rootFrame.GoBack();           

    }

    I am a bit confused as this suggests to me it should go back up one page not to the root?

    Tim

    Hi,

    The fact is that the pages are adding to the app NavigationStack not as you call them (RootPage,DetailPage, etc..)they added on the principle of the stack ("FIFO" First in First Out). For example you have three pages Page1, Page2, Page3 , let's assume that when the app is starting the rootFrame navigate to Page1  by calling rootFrame.Navigate(typeof(Page1), e.Arguments) method now NavigationStack is empty,  after from Page1 you navigate to Page2 using rootFrame.Navigate(typeof(Page1), e.Arguments) method during this method calling the Page1 is adding to NavigationStack and NavigationStack contain only 1 element which is Page1. If from Page2 you call rootFrame.GoBack() then the go back method check which page added to NavigationStack latest and go to that page in our case it is Page1 on the principle of the stack ("FIFO").

    Thus if we navigate from Page1 to Page2 then to Page3 and call rootFrame.GoBack() we go back to the Page2,

    In your scenario you go from Page1 to Page3 and want go back to Page2 from Page3 but from Page3 if you use rootFrame.GoBack() you go to Page1.


    Make the community better together

    Monday, August 15, 2016 9:33 PM
  • Thus if we navigate from Page1 to Page2 then to Page3 and call rootFrame.GoBack() we go back to the Page2,

    Maybe I wasn't clear in my explanation this is what I am doing and when I call the code I goback() to page 1?
    Monday, August 15, 2016 9:42 PM
  • Thus if we navigate from Page1 to Page2 then to Page3 and call rootFrame.GoBack() we go back to the Page2,

    Maybe I wasn't clear in my explanation this is what I am doing and when I call the code I goback() to page 1?

    You go form Root Page to List Page then from List Page to Detail Page and when you calling GoBack from Detail Page you go back to Root Page ? Can you share with me which code you use to go form Root Page to List Page then from List Page to Detail Page

    Make the community better together

    Monday, August 15, 2016 9:54 PM
  • Here it is

    This is Root Page to List Page

    private void balance_Tapped(object sender, TappedRoutedEventArgs e)
            {
                this.Frame.Navigate(typeof(Views.ItemList));
            }

    Then List Page to Detail Page

     private void ListViewSelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                var button = sender as ListView;
                var item = button.SelectedItem as BillingItem;
                if (item != null)
                {
                    this.Frame.Navigate(typeof(Views.EditItem), item.Id);
                }
            }

    Monday, August 15, 2016 9:58 PM
  • Are you sure that in the both method you use the same frame instance?

    Try use following code

    private void balance_Tapped(object sender, TappedRoutedEventArgs e) {

    Frame rootFrame = Window.Current.Content as Frame; rootFrame.Navigate(typeof(Views.ItemList)); }

    private void ListViewSelectionChanged(object sender, SelectionChangedEventArgs e) { var button = sender as ListView; var item = button.SelectedItem as BillingItem; if (item != null) {

    Frame rootFrame = Window.Current.Content as Frame; rootFrame .Navigate(typeof(Views.EditItem), item.Id); } }




    Make the community better together


    Monday, August 15, 2016 10:11 PM
  • I replaced my code for the code above and see the same behaviour, it goes back to the root frame.

    Thanks

    Tim

    Monday, August 15, 2016 10:16 PM
  • Can you share all code, if not then can you reproduce your issue in new project and share with it?

    Make the community better together


    Monday, August 15, 2016 10:31 PM
  • Make sure you don't have multiple go back handlers installed. It would help if you'd tag your subject line with the type of app you are writing as requested in the forum sticky notes. The details may be subtle different for different app types, especially if you're using different base navigation templates.
    I'd you put headlights on the intermediate pages navigation events so they get hit or does the page get totally skipped?
    Tuesday, August 16, 2016 4:09 AM
  • Apologies have updated the title.

    This pointed me in the right direction, at somepoint I must have stripped the async from the OnNavigatedTo

    protected override void OnNavigatedTo(NavigationEventArgs e)
    When I added it back

    protected async override void OnNavigatedTo(NavigationEventArgs e)
    It all kicked into life

    • Marked as answer by Tim_D_Jordan Tuesday, August 16, 2016 4:57 AM
    Tuesday, August 16, 2016 4:57 AM