The original: http://www.wpgamer.info/2013/05/back-button-in-xna.html
If you want to create a game for Windows Phone, then XNA is a choice. Now, I will explore and learn XNA for Windows Phone with you.
If you want your games pass the review of Windows Phone Marketplace, then you need to do some special processing for Back button.
If the user press the Back button when playing, you should ask the user whether to exit current level and pause the game.
If the user press the Back button on the home screen, then you should ask the user whether or not to quit the game.
In the default case, the class Game in your project already contains codes about the Back button.
protected override void Update ( GameTime gameTime ) { if ( GamePad.GetState ( PlayerIndex.One ).Buttons.Back == ButtonState.Pressed ) this.Exit ( ); base.Update ( gameTime ); }
GamePad is a class for game pad. Using GetState method to get a player's game pad. But in Windows Phone, we just need to get the game pad of player I.
GamePad
GetState
The GetState method returns the class GamePadState, which is the game pad status. Property Buttons is the buttons on the game pad, in the above code, we get the property Back, which is the state of the Back button. If the Back button is pressed, then we exit the game.
GamePadState
In the above code, if you press the Back button, then you will exit the game. So we need to add some codes that ask the user whether to exit.
protected override void Update ( GameTime gameTime ) { if ( GamePad.GetState ( PlayerIndex.One ).Buttons.Back == ButtonState.Pressed ) Guide.BeginShowMessageBox ( "Exit", "Do you want to exit?", new string[] { "Yes", "No" }, 1, MessageBoxIcon.None, new AsyncCallback ( this.userSelected ), null ); base.Update ( gameTime ); } private void userSelected ( IAsyncResult result ) { if ( !result.IsCompleted ) return; int? index = Guide.EndShowMessageBox ( result ); if ( index.HasValue && index.Value == 0 ) this.Exit ( ); }
We use the BeginShowMessageBox method of Guide class to pop up a dialog box that lets the user choose whether or not to quit the game. The method userSelected is used to determine the user's choice. They can choose Yes or No.
BeginShowMessageBox
Guide
userSelected
In the method userSelected, parameter result indicates the user's selection, property IsCompleted indicates whether the user completed the selection. You can use EndShowMessageBox method of Guide to get the button index. In our code, and 0 means Yes. Note the variable index, it is not int, but is int?.
IsCompleted
EndShowMessageBox
We can add two more fields to represent whether the player is playing and the game is paused. According to these two variables to display different dialog boxes.
On Windows Phone 7.x devices, we pop up a dialog box when the user presses the Back button, if users press the Back button again before the dialog box is displayed, the BeginShowMessageBox might throw an exception. If you want to avoid this error, you can set a field to determine whether the dialog box is displayed.
private bool isMessageBoxShow = false; protected override void Update ( GameTime gameTime ) { if ( GamePad.GetState ( PlayerIndex.One ).Buttons.Back == ButtonState.Pressed && !this.isMessageBoxShow ) { this.isMessageBoxShow = true; Guide.BeginShowMessageBox ( "Exit", "Do you want to exit?", new string[] { "Yes", "No" }, 1, MessageBoxIcon.None, new AsyncCallback ( this.userSelected ), null ); } base.Update ( gameTime ); } private void userSelected ( IAsyncResult result ) { this.isMessageBoxShow = false; if ( !result.IsCompleted ) return; int? index = Guide.EndShowMessageBox ( result ); if ( index.HasValue && index.Value == 0 ) this.Exit ( ); }
More contents, please visit Developer.