0

I want to handle the windows phone's back button event on several of my pages differently. My app's pages are hierarchically ordered and instead of exiting the app itself (what only should be possible on the hierarchical uppermost page => also known as home page) I want to navigate to the hierarchical upper-next page.

Hierarchical navigation:

WP Main Menu > App Home Menu > App Page#1 > App Page#2 > ...

I already defined a related method for handling the back button press but I can't seem to be able to register the handler within the page's constructor accordingly, and there are no related C++/CX guides out there!

private:
void Page::HardwareBackButtonPressed(Platform::Object^ sender,
    Windows::Phone::UI::Input::BackPressedEventArgs^ e)
{
    e->Handled = true;
    if (Frame->CanGoBack == true)
        Frame->GoBack();
    else
        Frame->Navigate(MainPage::typeid, safe_cast<Platform::Object^>(0));
}

The registration of the handler looks like this (seems to be invalid)

HardwareButtons::BackPressed += ref new Windows::Foundation::EventHandler<BackPressedEventArgs^>(
    this, &HardwareBackButtonPressed);

I'm not even sure if this is the right way to go.

neuronalbit
  • 358
  • 1
  • 3
  • 15
  • This seems like a really good way to infuriate your users! – jonrsharpe Mar 20 '15 at 13:08
  • You don't understand, my application consists of several hierarchical ordered pages. If you go deeper on another page, you should go back to the upper-next hierarchical page till the main/home page is reached. From that point you can press the back button and switch to your phone's main screen as usual. Beside that it would be great if you could just answer the question and not questioning why I want to do this. – neuronalbit Mar 20 '15 at 13:20
  • http://stackoverflow.com/questions/24414570/windows-phone-8-1-backpressed-not-working-properly – Hans Passant Mar 20 '15 at 13:24
  • Then perhaps you should say that, which doesn't match with *"on one of my pages differently"*. *"Beside that it would be great if you could just answer the question and not questioning why I want to do this"* - passive aggression is not terribly constructive when you're trying to get people to help you for free. – jonrsharpe Mar 20 '15 at 13:25
  • 1
    Sry. for not mentioning this but I'm kind of new to this whole wp8 stuff and it just drives me nuts that there are almost zero c++/cx guides on the net related to this and the MSDN documentation has still more c# examples than c++/cx examples. – neuronalbit Mar 20 '15 at 13:31
  • The description you've added looks like a default behavior. You don't need to override it, the back navigation will work as you expect. – Anton Sizikov Mar 20 '15 at 13:45
  • In my case it doesn't, the app switches directly to the wp8 screen. On my home page I have a pivot element and within a given pivot item there are button elements. If I press one specific button I get to another (hierarchical deeper) page which also holds a pivot element. The navigation takes place through this code "Frame->Navigate(SecondPage::typeid, 1);" where I pass an index value that gets treated by the NavigatedTo method of the new page, in order to switch to a specific pivot item. I guess this is the problem! – neuronalbit Mar 20 '15 at 13:56

1 Answers1

1

Alright, after having a indeep look on the generated NavigationHelper class I was able to fix the problem by myself.

In order to implement a page specific behaviour for the hardware back button you need to register an according event handler within the OnNavigatedTo method.

_hardwareBackPressedEventToken = HardwareButtons::BackPressed += ref new EventHandler<BackPressedEventArgs^>(
    this, &thisPage::HardwareBackButtonPressed);

Furthermore, in case you want to have another or the default behaviour, you need to use a EventRegistrationToken in order to deregister the current handler first. In my case - for navigating back to the hierarchical upper-next page - this is done within the OnNavigatedFrom method.

HardwareButtons::BackPressed -= _hardwareBackPressedEventToken;
neuronalbit
  • 358
  • 1
  • 3
  • 15