1

In my page.xaml, i have hooked the Back hardware button as this:

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

and implement the method:

private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
    {
        // Handle the Virtual Hardware Button: Back,
        // When user taps it, I need to get the previous page name. 
        System.Diagnostics.Debug.WriteLine("CurrentSourcePageType = " + Frame.CurrentSourcePageType.FullName);
        System.Diagnostics.Debug.WriteLine("Back button is pressed...");
    }

But here, the Frame.CurrentSourcePageType.FullName is already the back navigated page name, how can i get the previous page name?

Maybe I need to describe my question better here: Suppose I have 2 pages A and B, Through page A I navigate to page B, in the page B I have done something, then I only want to use back button to trigger some customize action (I don't want to add button in my Page), but this action needs to get the page B's name first.

Rui Huang
  • 382
  • 1
  • 5
  • 18

1 Answers1

2

First of all, HardwareButtons.BackPressed is an app-wide event, so it's not a good idea to subscribe to it in Page, unless you do it very carefully and remember to unsubscribe when not needed any more. It's worth also to mention that you should pay special attention if anything has been susbscribed to this event before your Page - for example in app's constructor or anywhere else. (some typical places are shows here in Igrali's answer).

To do what you want you can for example either build an app-wide event that will be responsible for an action, or use the code as shown below (following Rob Caplan's answer). In both cases the name (type) of your previous page you can get from Frame.BackStack.

RelayCommand myGoBackCommand;

public BasicPage1()
{
    this.InitializeComponent();

    this.navigationHelper = new NavigationHelper(this);
    this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
    this.navigationHelper.SaveState += this.NavigationHelper_SaveState;
    myGoBackCommand = new RelayCommand(() => GoBackAction());
    this.navigationHelper.GoBackCommand = myGoBackCommand;
}

private void GoBackAction()
{
    //print previous page name before going back
    Debug.WriteLine(Frame.BackStack.Last().SourcePageType);
    if (navigationHelper.CanGoBack())
        navigationHelper.GoBack();
}

Note: To make the above code work, delete all other subscriptions to HardwareButtons.BackPressed and make your pages BasicPages (use navigation helper and so on). Of course you can have other subscriptions, but you need to handle them carefully, in most cases these events are fired with order they were subscribed.

Remark - in case you need also handle the case when user navigates back from other Page and you want the name of that page - then recognize type of navigation in OnNavigatedTo (forward/back navigation) and then read suitable Frame's stack - BackStack or ForwardStack.

The code above is using BasicPage template - if you don't have mentioned classes, then add to your project a new BasicPage and VS should ask you if you want to add the common files (NavigationHelper ans so on).

Community
  • 1
  • 1
Romasz
  • 29,662
  • 13
  • 79
  • 154
  • your answer gives quite good details of handling hardware back button. I followed your idea and got exception when I pressed the back button: An exception of type 'System.InvalidOperationException' occurred in SYSTEM.CORE.NI.DLL but was not handled in user code, it throws at Debug.WriteLine(Frame.BackStack.Last().SourcePageType); – Rui Huang May 17 '15 at 15:58
  • @RuiHuang Have you tried to debug your program? Maybe the problem is that the BackStack is empty? (of course it will need such check-up) – Romasz May 17 '15 at 20:06
  • YES, the exception is due to the empty back stack, i removed all back stack for the page when i navigated to it, thanks for you comment but i am still stuck here, i added more description in my original post. – Rui Huang May 17 '15 at 20:11
  • @RuiHuang Description 'I've done something' doesn't help much, especially if you have emptied the *BackStack*. In your case why don't you make a variable which will hold the last page name? - you can put there the name just before you empty the backstack. Then you can use this variable in back button. In this case you can also handle forward naviagation easily. – Romasz May 18 '15 at 04:39
  • Thanks, Yes, that name variable helps. – Rui Huang May 18 '15 at 15:26
  • @Romasz Is there any way to handle BackButton press in case of Blank Page Template. I have created almost 20 pages with Blank Template and now I started facing the back key similar issue. – Kinjan Bhavsar Jan 18 '16 at 16:09
  • @KinjanBhavsar You can subscribe to the event when app is launched in app.xaml.cs for example - then it will work for the whole app and every page. – Romasz Jan 18 '16 at 17:15
  • Thanks will it cause any issue?? – Kinjan Bhavsar Jan 19 '16 at 17:32