Search
Close this search box.

Properly Exiting Silverlight-based WP7 Games

Anyone who has read the Windows Phone 7 Application Certification Requirements (PDF) knows that a WP7 game can never be more than two taps of the Back button away from quitting the game. In XNA this is easy since the Game class provides a method called Exit that can be called with relative ease. In Silverlight nothing quite so easy exists. One clever hack that’s been making the rounds is to create a new instance of the XNA Game class and call its Exit method. However this solution, clever though it is, is still a hack and probably not the best way to approach things. So what else can the aspiring Silverlight WP7 game developer do?

Well one solution is to create a “V”-based solution. I’ve implemented a simple, yet full-featured one and made it available under the Microsoft Public License here: http://bouncesample.codeplex.com/. A “V”-based solution is one in which the start page (MainPage.xaml) acts as a routing page. When the user starts the game, it routes them to a MainMenu.xaml page. When they choose an option from that menu, rather than navigate directly to that option, a public static state variable is set in MainMenu.xaml’s code-behind and NavigationService.GoBack() is invoked instead. When the program returns to MainPage.xaml, the override of OnNavigatedTo triggers, checks the pre-set state variables, and routes to the appropriate page (e.g. OptionsMenu.xaml or GamePlay.xaml). In GamePlay.xaml, when the user presses Back, the OnBackKeyPressed override triggers and the state variable is set to redirect the user to the PauseMenu.xaml page. This page gives the user the option to resume the game, return to the main menu, go to the options menu, or quit the game entirely. As per the App Cert reqs, a press of Back here exits the game (again through an override of OnBackKeyPressed which sets the appropriate state). Choosing Options sets a special public static bool within the OptionsMenu.xaml code that tells it to both change the text on the return button to reflect that it will be returning to the pause menu and to return to the pause menu on a press of that button (and on a Back key press too).

Anyway, the code should be easy enough to follow – the interesting bits are all in the code behind files. It’s up to you to make it pretty and create a real game for it, of course. I suspect I’ll be returning back to XNA with my next post. But who knows. Until then, good luck!

This article is part of the GWB Archives. Original Author: Bob Taco Industries

Related Posts