16

In my Xaml Page I've got a Frame.

I'm trying to have a backButton event to just navigate inside frame .

so I tried to use this piece of code

public MainPage(){
    this.InitializeComponent();
    if(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")) {
        Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
    }
}
private void HardwareButtons_BackPressed(object sender,BackPressedEventArgs e) {
    if(insideFrame.CanGoBack())insideFrame.GoBack();
    else  Application.Current.Exit();
}

but In phone after doing HardwareButtons_BackPressed event it close the application.

It seems to running some default back button behavior on MainPage...

How can I fix it? And In Windows10 does they add new events to handle back navigation?


[Update]

Now I found out it's better to Use SystemNavigationManager in Windows 10 instead of Input.HardwareButtons.BackPressed.

SystemNavigationManager currentView = SystemNavigationManager.GetForCurrentView();
Shahriar
  • 939
  • 1
  • 14
  • 36
  • Use of `SystemNavigationManager` can be found in the following blog: http://www.wintellect.com/devcenter/jprosise/handling-the-back-button-in-windows-10-uwp-apps – NP3 Dec 27 '15 at 08:09
  • @Shahriar Will this work for Windows 10 Mobile also. Because I checked, back button is not displayed in Windows Mobile. – Kinjan Bhavsar Mar 17 '16 at 10:56

5 Answers5

24

Windows 10 (UWP) include SystemNavigationManager in Windows.UI.Core namespace for Navigation purpose only.

Because SystemNavigationManager is part of Windows Universal Platform, So, it's supported by all device family running on Windows 10 including Mobile and PC.

For Single Page


If you just want to handle navigation for single page. Follow the following steps

Step 1. Use namespace Windows.UI.Core

using Windows.UI.Core;

Step 2. Register back request event for current view. Best place for this is main constructor of class after InitializeComponent().

public MainPage()
{
    this.InitializeComponent();
    //register back request event for current view
    SystemNavigationManager.GetForCurrentView().BackRequested += MainPage_BackRequested;
}

Step 3. Handle BackRequested event

private void Food_BackRequested(object sender, BackRequestedEventArgs e)
{
    if (Frame.CanGoBack)
    {
        Frame.GoBack();
        e.Handled = true;
    }
}

For Complete Application at one place for single rootFrame


Best place for handling all backbutton for all Views is App.xaml.cs

Step 1. Use namespace Windows.UI.Core

using Windows.UI.Core;

Step 2. Register back request event for current view. Best place for this is OnLaunched just before Window.Current.Activate

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
    ...
    SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
    Window.Current.Activate();
}
    

Step 3. Handle BackRequested event

private void OnBackRequested(object sender, BackRequestedEventArgs e)
{
    Frame rootFrame = Window.Current.Content as Frame;
    if (rootFrame.CanGoBack)
    {
        rootFrame.GoBack();
        e.Handled = true;
    }
}

References- Handle back button pressed in UWP

Hope this is helpful to someone!

Community
  • 1
  • 1
Vineet Choudhary
  • 7,433
  • 4
  • 45
  • 72
7

You need to tell the system that you handled the backbutton press by setting the Handled property of the BackPressedEventArgs to true.

  private void OnHardwareButtonsBackPressed(object sender, BackPressedEventArgs e)
  {
        // This is the missing line!
        e.Handled = true;

        // Close the App if you are on the startpage
        if (mMainFrame.CurrentSourcePageType == typeof(Startpage))
            App.Current.Exit();

        // Navigate back
        if (mMainFrame.CanGoBack)
        {
            mMainFrame.GoBack();
        }
  }
Tobias Hoefer
  • 1,058
  • 12
  • 29
0

follow these steps:

  • Add two Global Variables in your page as below.

    private NavigationHelper navigationHelper;
    private RelayCommand _GoBackCommand;
    
  • Then add below code in constructor of specific page.

        // below code is to override the back navigation
        // hardware back button press event from navigationHelper
        _GoBackCommand = new RelayCommand
            (
                () => this.CheckGoBack(),
                () => this.CanCheckGoBack()
            );
    
        navigationHelper.GoBackCommand = _GoBackCommand;
        // ---------
    
  • Then add both those methods we've just declared in constructor.

    private bool CanCheckGoBack()
    {
        // this should be always true to make sure the app handles back buton manually.
        return true;
    }
    
    private void CheckGoBack()
    {
        // this will be execute when back button will be pressed
    }
    

ps. - for this you might need to use BasicPage instead of BlankPage while adding new page.

hope this will help..!

Keval Langalia
  • 1,762
  • 1
  • 16
  • 29
  • Where is `NavigationHelper ` ? As you know in Windows10 there is no template such windows phone 8.1 to have `NavigationHelper` inside it... If you know... where can I get `NavigationHelper `? – Shahriar Aug 05 '15 at 12:31
  • In windows 10 there is no `BasicPage` ... it's just `BlackPage` – Shahriar Aug 05 '15 at 12:32
  • you can get the file from any existing wp8.1 project or may be from other project template. – Keval Langalia Aug 05 '15 at 12:33
0

Try this.It will work for frame back navigation.

  protected override void OnNavigatedTo(NavigationEventArgs e)
    {

      HardwareButtons.BackPressed += HardwareButtons_BackPressed;

    }


    void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
    {

    Frame rootFrame = Window.Current.Content as Frame;

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

}
-1

I think this is because you add HardwareButtons_BackPressed in your page instead on in app.xaml.cs.

In app.xaml.cs :

public App()
{
    this.InitializeComponent();
    this.Suspending += this.OnSuspending;

    HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}

void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
    Frame rootFrame = Window.Current.Content as Frame;

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

Now, back button of your phone will work on any pages.

And then, if you want to add a particular button doing back in any page :

In the particular page (or each pages if you want) :

public void btn_return_Tapped(object sender, TappedRoutedEventArgs e)
        {
            Frame rootFrame = Window.Current.Content as Frame;

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

Source : http://windowsapptutorials.com/tips/general-tips/handling-the-back-button-in-a-windows-phone-8-1-app/

Clad07
  • 1
  • 2