Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I have just moved over to making apps for Windows Phone 8.1 using the RT APIs having formerly exclusively used Silverlight for Windows Phone apps.

I am creating a basic app with different pages. The main page is just a menu page with a list of buttons that take the user to different pages. The issue I am having is getting the user back to the main page from other pages.

I have two types of sub-pages.

The first ones are just plain pages and when the user hits the back button they get taken back to the main page.
However I have some pages with a pivot control. When the user hits the back button they are taken into the previous app. (My app is still running in the background).

Could someone explain why only the pages with pivot controls take the user out of my app when they hit the back button instead of the previous page (in this case main page)?

Any help is much appreciated, I have done some searching and I am aware that the navigation model has changed in Windows Phone 8 then in Windows Phone 8.1 RT apps to align them closer to regular Windows metro apps.

Here is my code for navigation.
C#
private void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
              this.Frame.Navigate(typeof(about));
                  // This goes to a page with no pivot control and the user can navigate back fine.
        }


        private void partAbutton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(parta));
                // This goes to a page with a pivot control and the user cannot navigate back the main page when they hit the back button.
        }
Posted
Updated 29-Dec-14 23:59pm
v2

1 solution

You have to set the BackPressedEventArgs.Handled property to true. If your app is at its first page and can't navigate backwards, you should not handle the event and the operating system will suspend your app.
First you have to enable the NavigationCacheMode and then add an event handler for back button press to OnNavigatedTo method of the page and in the method you can navigate to back page by using the navigation stack.
Here is the complete code for that.

C#
public BlankPage2()
  {
      this.InitializeComponent();
      this.NavigationCacheMode = NavigationCacheMode.Enabled;
  }


  protected override void OnNavigatedTo(NavigationEventArgs e)
  {


      HardwareButtons.BackPressed += this.HardwareButtons_BackPressed;
  }


  protected override void OnNavigatedFrom(NavigationEventArgs e)
  {
      HardwareButtons.BackPressed -= this.HardwareButtons_BackPressed;
  }



  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;
      }
  }
 
Share this answer
 
Comments
Gerson Cardoso Filho 27-Nov-15 15:00pm    
Man, thank you very much. You simply save my life. You´re the guy.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900