Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5174830
  • 博文数量: 1696
  • 博客积分: 10870
  • 博客等级: 上将
  • 技术积分: 18357
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-30 15:16
文章分类
文章存档

2017年(1)

2016年(1)

2015年(1)

2013年(1)

2012年(43)

2011年(17)

2010年(828)

2009年(568)

2008年(185)

2007年(51)

分类: 嵌入式

2010-11-26 10:41:49

Properly Exiting Silverlight-based WP7 Games

Anyone who has read the 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: . 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!

阅读(677) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~