I am developing a simple game for windows phone and I got stuck when at the handling the back button press.

I have been searching for a while on google but I have not found anything that could work for me. I have seen a function GamePad but i couldn't find on which namespace is this function located, and if it is working on WP8.1.

So my question is, how can I set up a function in which I can handle some code when user press back button.


  • \$\begingroup\$ stackoverflow.com/questions/14466938/… \$\endgroup\$
    – dimitris93
    Commented Feb 1, 2015 at 10:28
  • 1
    \$\begingroup\$ Yes, but when I try to override the the OnNavigatedFrom method on build i get this error: 'Error 1 Game1.GamePage.OnNavigatedFrom(Windows.UI.Xaml.Navigation.NavigationEventArgs)' is a new virtual member in sealed class 'Game1.GamePage' I am using the new Monogame template for windows phone 8.1 which is not silverlight but runtime. \$\endgroup\$
    – Edvin
    Commented Feb 1, 2015 at 13:58

1 Answer 1


I too couldn't wrap my head around this, as there is no mention of it in the documentation whatsoever. I also tried every single thing I could google and in the end, I figured it out by chance.

It's quite simple, really:

GamePadState padState = GamePad.GetState(PlayerIndex.One);
if (padState.Buttons.Back == ButtonState.Pressed) {
    // Do your stuff


I had some major issues with it for what appeared to be two incomprehensibly related reasons: firstly, be sure to call this in your update as soon as you can. Calling it on the end of my update method made it work only sometimes (probably for some other reason, but I couldn't figure it out).

And secondly, keep one global instance of GamePadStatefor use in all classes that might have some control logic - I had two classes that registered control (with their own GamePadState instances) and while one did register the back button, the other didn't - and when I "paused" the update of the first one, the second one started to register it. Beats me.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .