3
votes

I'm updating my app from Windows Phone 8 Silverlight to Windows 8.1 RT (I think is called that).

I've just created my second page and when i go to and press the back button it goes out of my app instead of going back to first page.

I don't know why is this happening, default behaviour is going to last page right?

I can't find how to override back button event to make a Frame.GoBack() call.

Is this a dev preview bug or am I missing something?

2
@igrali HardwareButtons class isnt supported on universal apps, only Windows Phone 8. link that question isnt solving anything...Nanoc
You should check your facts before saying that something is not solving anything - that link says that the minimum version is Windows Phone 8.0. My solution works for Universal Apps, and your questions is still a duplicate.Igor Ralic
Ok i see you can make universal apps since windows 8, i was thinking was new on 8.1, anyway Visual Studio tells me that there arent any HardwareButtons class, some way to solve it?Nanoc
Make sure it's inside #if WINDOWS_PHONE_APPIgor Ralic

2 Answers

5
votes

put into the constructor of the second page: (SecondPage.xaml.cs)

Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;

and then define the eventhandler function:

    private void HardwareButtons_BackPressed( object sender, BackPressedEventArgs e )
    {
        Frame.GoBack();
        e.Handled = true;
    }
-1
votes

In the Universal Windows Apps you can also handle "Back Button" click globally in the App.xaml.cs file. Please see below:

  1. In the OnLaunched method add below code:

    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
      //..Rest of code...
    
       rootFrame.Navigated += OnNavigated;
    
    
        // Register a handler for BackRequested events and set the
        // visibility of the Back button:
    
        SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
    
        SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
            rootFrame.CanGoBack  ?
            AppViewBackButtonVisibility.Visible :
            AppViewBackButtonVisibility.Collapsed;
    
        //..Rest of code...
    
    }
    

Then you should add code for handlers methods in the App class:

    private void OnNavigated(object sender, NavigationEventArgs e)
    {
      // Each time a navigation event occurs, update the Back button's visibility:

        SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
        ((Frame)sender).CanGoBack ?
        AppViewBackButtonVisibility.Visible :
        AppViewBackButtonVisibility.Collapsed;
    }



   private void OnBackRequested(object sender, BackRequestedEventArgs e)
   {
      Frame rootFrame = Window.Current.Content as Frame;

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