Equipment navigation and return button

I am trying to use the back button in a Windows Phone 8.1 application. When I run the application I am on page A. Then I go to page B - it works well, but when I go:

A → B → C and press the back button to go to A

also when A → B → C → D and back - also go to A

I used this code for navigation:

Frame.Navigate(typeof(StartingView));

      

and use this code to implement the hardware return button:

HardwareButtons.BackPressed += HardwareButtons_BackPressed;

Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame != null && rootFrame.CanGoBack)
{
    rootFrame.GoBack();
    e.Handled = true;
}

      

on page D I am listing Frame.BackStack and it looks good. Also when I create a button with:

Frame.GoBack();

      

it goes from D -> C, but when I use hardware it goes to A.

Any idea?

+3


source to share


3 answers


I decided to add it to App.xaml.cs in the constructor:

HardwareButtons.BackPressed += HardwareButtons_BackPressed;

      

and then



private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
    {
        Frame frame = Window.Current.Content as Frame;
        if (frame == null)
        {
            return;
        }

        if (frame.CanGoBack)
        {
            frame.GoBack();
            e.Handled = true;
        }
    }

      

And important: removed this whole handler from other views.

+4


source


Have you checked the Backstack entry?

Check if the pages are stacked.



string myPage = "";

foreach (PageStackEntry page in Frame.BackStack)
{
    myPage = page.SourcePageType.ToString();
}

      

Now look at the "mypage" line on each page and make sure that after you navigate from each page, you get the name of the page you are navigating from.

0


source


I noticed something that might help. If you are using "Main Pages" rather than "Blank Pages", you can remove "hardware back" from your App.xaml.cs. This should fix the problem.

0


source







All Articles