locked
Get the requested page when pressing back button RRS feed

  • Question

  • Hi, Is it anyway to get the value of the page when pressing back button, either the page navigated from or navigating to ..

    // I am developing a universal app, and I will use the code in app.xaml.cs

    Wednesday, August 10, 2016 6:23 PM

Answers

  • Hi MrBrilliant12,

    >>Is it anyway to get the value of the page when pressing back button, either the page navigated from or navigating to ..

    It seems it can not. There is an official document about the Back button navigation, please refer it in this link: https://msdn.microsoft.com/en-us/library/windows/apps/mt465734.aspx.

    When we press the back button, the SystemNavigationManager.BackRequested will be fired. There is no property to get the page info of the event.

    But we can use the Frame.BackStack to get the page in the back stack.

    For example:

    private void App_BackRequested(object sender, BackRequestedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;
        if (rootFrame == null)
            return;
        if (rootFrame.CanGoBack && e.Handled == false)
        {
            var list = rootFrame.BackStack;
            foreach (var item in list)
            {
             var pageInfo=item.SourcePageType;
            }
    
            e.Handled = true;
            rootFrame.GoBack();
        }
    }
     

    Best Regards,

    Jayden Gu

    • Proposed as answer by Jayden Gu Tuesday, August 23, 2016 1:56 AM
    • Marked as answer by MrBrilliant12 Tuesday, August 23, 2016 8:19 AM
    Thursday, August 11, 2016 12:32 PM