Highlights of XNA development on Windows Phone 7

DZone 's Guide to

Highlights of XNA development on Windows Phone 7

· Mobile Zone ·
Free Resource

Developing games for multiple platforms might be a daunting task when it comes to porting the same project to a different system while still maintaining its capabilities. Ever since Microsoft released the XNA framework, this task has been simplified and developers who created products for Microsoft platforms were able to build one game and then compile it to multiple platforms (Xbox 360, Windows, Zune) with little or no code changes. The same simplicity is now available for those who plan to develop games for Windows Phone 7. XNA Framework is available on this platform as well, giving developers access to many of the unique phone features like the accelerometer or GPS.

It all begins with the SDK installation. XNA Game Studio is a component part and will be automatically downloaded and installed with all other Windows Phone 7 development tools. Of course, you can also download it as a standalone package. Once ready, if you open Visual Studio 2010, you will see that there are a bunch of new project types available:

Once a test project is created, the basic code structures are absolutely the same in any other XNA game:

One important difference, however, is the fact that now you have very strict hardware considerations to follow. Windows Phone 7 is not even close performance-wise both to a PC or an Xbox 360, so you must be very careful with object allocations and complex graphics.

Assuming that you are already familiar with the basics of XNA development, you know that by default there are five methods present in the game class:

  • Initialize
  • LoadContent
  • UnloadContent
  • Update
  • Draw 

The Initialize method is triggered the moment the game starts on the device. It is the fundamental starting point and generally a developer should not interfere with the initialization process. LoadContent is designed to allow the developer to load additional content resources like textures in order to be able to use those later in the game. UnloadContent, on the other side is the method that allows you to dispose the used content.

Update and Draw are, in my opinion, two the most interesting methods. Update is constantly running while the game is executing - that's where you are able to check for user input and define specific game element behavior depending on the game state. Draw is also constantly running while the game is loaded, but its main purpose is to draw game elements depending on the current game state.

Let's see this in action when I want to track user gestures. Unlike Xbox 360 and PC, Windows Phone 7 relies on touch input. The XNA Framework 4.0 comes pre-built with a new namespace -  Microsoft.Xna.Framework.Input.Touch. By default, the touch gestures are not handled. To change this, in the game class constructor I am defining what I need to track by setting the EnabledGestures property of the TouchPanel class.

XNA games have a different structure compared to Silverlight applications. In a regular client application there are pages that can be particular touch input panels. In an XNA game, there is one screen that is not directly handled by the developer. Therefore, TouchPanel is a static class that manages the touch input for the entire game.

There are 11 gesture types that can be tracked by the TouchPanel. I am not going to document those here since the official documentation is pretty detailed on this topic. As I said before, I can declare the needed gestures in the class constructor:

public Game1()
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";

// Frame rate is 30 fps by default for Windows Phone.
TargetElapsedTime = TimeSpan.FromTicks(333333);
TouchPanel.EnabledGestures = GestureType.VerticalDrag | GestureType.Tap | GestureType.PinchComplete | GestureType.Pinch
| GestureType.HorizontalDrag | GestureType.Hold | GestureType.FreeDrag | GestureType.Flick;

Here I am specifying that there are multiple gesture types I want to track, and this might serve different purposes in a game. For example, a vertical drag could draw a trajectory for a ball and a hold might trigger the on-screen menu. 

Once set, I can track the execution of specific gestures in the Update method:

protected override void Update(GameTime gameTime)
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

if (TouchPanel.IsGestureAvailable)
if (TouchPanel.ReadGesture().GestureType == GestureType.VerticalDrag)
Debug.WriteLine("There was a vertical drag!");


First of all, I am checking whether a gesture is available, and if so - I need to check the gesture type.

Now that you are aware of how basic gestures are handled let's put this functionality to use and at the same time learn about another XNA-specific feature - the ability to access the media library. It is fair to exepct the end-user to hold some media content on his device - for example, songs. 

The entire media library is exposed to the developer via the MediaLibrary class. In my game project, I want to access a specific song and play it when the user holds the finger on the screen (triggers the Hold gesture). I already have the basic structure available in the UpdateMethod and all I need to do is change the gesture type that I am checking against.

if (TouchPanel.IsGestureAvailable)
if (TouchPanel.ReadGesture().GestureType == GestureType.Hold)


Once the gesture is detected I need to instantiate the MediaLibrary class:

MediaLibrary lib = new MediaLibrary();

This class has an additional constructor that accepts a MediaSource instance as a parameter. If the phone you are developing doesn't have external storage (like an SD card), then all you have is a Local device library.

NOTE: You need an actual physical Windows Phone 7 device to test MediaLibrary capabilities. You can test basic functionality in the emulator, but in order to have access to sound and video media you will need a phone.

You can easily check the available storage devices with this piece of code:

IList<MediaSource> q = MediaSource.GetAvailableMediaSources();
foreach (MediaSource n in q)

The local device storage is used by default if it is not explicitly defined. Since I already have my phone connected, I want to find a song and play it. A MediaLibrary instance can offer access to the following content on the device:

  • Albums
  • Artists
  • Genres
  • Pictures
  • Playlists
  • Songs
  • SavedPictures

And although the playback of the content listed here requires the standard system media layer to be activated (e.g. songs), it gives the developer a lot of freedom as to what you can do with the media. For obvious reasons you cannot edit the media contents but you can access them.

For an example, I want to get the first song from my trance collection. I use a standard LINQ query and a call to the static MediaPlayer class, specifically designed for song playback.

Song n = (from c in lib.Songs where c.Genre.Name == "Trance" select c).First();

This is a good way to add some media interactions to a game. The same way you can obtain album art and additional media information stored in ID3 tags. I would not recommend abusing this sort of functionality because users don't want to have media content accessed without their consent.

There's a lot more to the XNA Framework besides the abstraction layer it offers on top of the mobile platform. When working with XNA for mobile development, additional restrictions will be enforced - for example, you can't use custom shaders. For most games that you are going to be develop, that is not a critical necessity and it was removed for performance considerations. As a developer, you have to understand that missing functionality is missing for a reason.   Mobile hardware performance is continuously improving, so more features will be available as handset makers continue to innovate.

Generally, I see no difference developing XNA games on WP7 compared to any other platform that supports the framework. Before actively developing a product, I would recommend checking this document.

An extremely good resource to check for XNA development is the AppHub educational portal.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}