
This document explains why and how to move your Windows Phone game to an architecture that integrates Silverlight with the XNA Framework.
While you can continue to use the Game class in XNA Game Studio 4.0 Refresh to build games for Windows Phone, moving to an application that integrates Silverlight with XNA Framework for your game brings a number of benefits. You get Silverlight’s navigation model for free, your game can leverage Silverlight UI elements, and a number of previously blocked scenarios are now possible, such as using a WebBrowser control to perform Open Authentication with social network integration.
Moving to this new model does have some trade-offs—namely, the Game class and related functionality, such as GameComponents, are not present. The rest of this guide attempts to answer the common questions about how to migrate to the new application model. It also acts as an educational tool for starting in the new model to draw some parallels back to your existing knowledge.
Where do put the things that used to be in your Game constructor? The answers vary based on the code. Following is a short breakdown based on what exists in our templates:
When migrating to the integrated Silverlight/XNA Framework model, there is no primary LoadContent method. In general you can load content any time you need; however, you need to ensure that you load graphical content only when the application is in XNA Framework rendering mode (by calling the SetSharingMode method and passing in true). If the application is in the Silverlight rendering mode, you will get exceptions if you try to load graphical content. All non-graphical content can be loaded at any time once you have a ContentManager.
In our template code, we point out that you can load content in the OnNavigatedTo method of the page, after enabling Game Studio rendering. This is a suggestion, because it is a place where you can safely load graphical content; however, it is not a requirement.
We now provide a simple GameTimer object to handle your update and draw loop. Following is the GamePage from the Windows Phone Rich Graphics Application template that provides a similar update-draw loop to the Game object:
C#
VB
The benefit of using GameTimer in these ways is that you can have as many running as you want, which means you can use GameTimer for anything from your main game loop to a timer that controls spawning enemies. Additionally, since GameTimer uses events, you can have multiple methods registered for drawing, which can make it easier to write separated drawing methods for different parts of your game.
This depends on the usage for the fields. In the Silverlight application model, you split your game or application into multiple pages, each one with a set of fields and methods for interacting at that point. For data that needs to be maintained and accessible throughout the entire application, you’ll want to use the App class in App.xaml.cs, created by the template. App can be retrieved at any point by using Application.Current, a static property, and casting it to your App type, as demonstrated in our template for retrieving the ContentManager:
C#
VB
One of the first things likely to cause trouble when migrating game logic code is the use of the GameTime class for update logic. Because GameTime is closely tied to the Game class, in the Silverlight/XNA Framework integrated model, you now have GameTimerEventArgs, which contains the same timing information as GameTime. If you have game logic that uses GameTime today, you can easily make that code portable between the Game class and GameTimer by changing the usage from something like this:
C#
VB
to instead directly take in the elapsed and total time as TimeSpans, which can be retrieved from both GameTime and GameTimerEventArgs:
C#
VB
This gives your game logic a common base that can be used from either app model, giving you greater portability between the two application models.
Note that “game logic” refers to extra classes that perform update logic; your primary update method will still take in either a GameTime object (in the Game class model) or a GameTimeEventArgs (in the Silverlight/XNA Framework model).
Many games built on the Game class also use GameComponents for some or all of their game logic. Unfortunately those types are tied to the Game class, so like GameTimer, they are not used in the integrated Silverlight/XNA Framework model. There are a few solutions to this problem.
One solution is to remove the GameComponent base type and simply make all methods public. Then your pages can simply create a list of the objects and use them as needed. This has the advantage of being lower cost, because it doesn’t require a lot of reimplementation of code, but it does mean your pages may have to do more work in order to leverage them.
Another solution is to modify your types such that you can pass them a GameTimer, and they will subscribe to the Update and Draw events. This will enable you to have objects that hook into an existing update/draw loop, but it does make those types tied to the Silverlight/XNA Framework model which can be a problem if you want to share those types with a Game-based game for Windows or Xbox 360.
The last solution is to reimplement the base types and interfaces for the GameComponent system. This requires more understanding of how GameComponent works, but that understanding would enable you to share components between your standard Game-class based games and integrated Silverlight/XNA Framework games.
There is also a new sample that provides an example of how one could architect a game component system in the Silverlight/XNA Framework model. The sample is not 100% compatible with the default GameComponent model (due to the removal of the Game and GameTime types), but you can use this new system in games based on either Silverlight/XNA Framework or the Game class.
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/