3

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?

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
Nanoc
  • 2,381
  • 1
  • 20
  • 35
  • possible duplicate of [Windows Phone 8.1 Universal App terminates on navigating back from second page?](http://stackoverflow.com/questions/24335925/windows-phone-8-1-universal-app-terminates-on-navigating-back-from-second-page) – Igor Ralic Jul 03 '14 at 16:07
  • @igrali HardwareButtons class isnt supported on universal apps, only Windows Phone 8. [link](http://msdn.microsoft.com/en-US/library/windowsphone/develop/windows.phone.ui.input.hardwarebuttons.aspx) that question isnt solving anything... – Nanoc Jul 03 '14 at 16:13
  • 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 Jul 03 '14 at 16:52
  • 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 Jul 03 '14 at 16:57
  • 1
    Make sure it's inside #if WINDOWS_PHONE_APP – Igor Ralic Jul 03 '14 at 17:00
  • Ok, i was thinking it will just throw an exception if i use something i cant, but it seems than i need that to access the Windows.Phone namespace, good to know it, VS force me to use full namespace or it tell me that class doesnt exists. Really thanks. – Nanoc Jul 03 '14 at 17:14
  • I think you are adding blank page, add basic page and it will handle all. – Yawar Jun 15 '15 at 10:04

2 Answers2

5

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;
    }
mimipofi
  • 655
  • 6
  • 6
-1

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();
      }
   }
Daniel Krzyczkowski
  • 2,732
  • 2
  • 20
  • 30