Windows Phone 7 beta: Debugging Tombstoned XNA Games with Visual Studio

In my previous posts on Windows Phone 7 I’ve been focussing on Silverlight. You can of course build applications and games using XNA – not being that familiar with XNA it’s going to take a lot of encouragement for me to invest serious time into learning another framework but if I was building games I think it’s a technology that is worth knowing since you can build games for Windows, XBox and now Windows Phone.

One of the things that you may notice when you build and deploy your XNA game to the emulator is that it doesn’t appear in the applications list. That’s because it is installed as a game, not an application. As such it appears in the Games hub, rather than in the applications list. If you use one of the hacked emulator builds you can see this hub and use it to relaunch your application without the debugger attached if you feel like it.

Ok, so onto the topic of this post. The last couple of posts have been about how you accommodate for the Windows Phone application lifecycle model, specifically the fact that your application may (and in the case of the current emulator, will) get terminated when it goes into the background. In these posts we used the Launching, Activated and Deactivated events on the Application (wired up in the App.xaml) and we relaunched Visual Studio when necessary in order to resume the debugging experience. This is essentially the same process for XNA with some minor differences.

Firstly, there is no App.xaml file, so you have to manually wire up these events. Also, by default there is no project reference to Microsoft.Phone.dll, so you’ll have to add that too. Then in the Game1.cs file (or in my case the MyGame.cs file, since I renamed the game from Game1 to MyGame) you need to attach the necessary event handlers. Note that I’ve wrapped the event wiring and the event handlers themselves with a check for the WINDOWS_PHONE conditional compilation symbol. Remember that you can target multiple platforms with the same XNA game – each target has a different compilation symbol: WINDOWS, XBOX, and now WINDOWS_PHONE.

public MyGame()
        {
            graphics = new GraphicsDeviceManager(this);

            //Set the Windows Phone screen resolution
            graphics.PreferredBackBufferWidth = 480;
            graphics.PreferredBackBufferHeight = 800;
            graphics.IsFullScreen = true;

#if WINDOWS_PHONE
            Microsoft.Phone.Shell.PhoneApplicationService.Current.Launching += new EventHandler<Microsoft.Phone.Shell.LaunchingEventArgs>(Current_Launching);
            Microsoft.Phone.Shell.PhoneApplicationService.Current.Deactivated += new EventHandler<Microsoft.Phone.Shell.DeactivatedEventArgs>(Current_Deactivated);
            Microsoft.Phone.Shell.PhoneApplicationService.Current.Activated += new EventHandler<Microsoft.Phone.Shell.ActivatedEventArgs>(Current_Activated);
#endif
            Content.RootDirectory = "Content";

            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromSeconds(1/30.0);
        }

#if WINDOWS_PHONE
        void Current_Launching(object sender, Microsoft.Phone.Shell.LaunchingEventArgs e)
        {

        }

        void Current_Activated(object sender, Microsoft.Phone.Shell.ActivatedEventArgs e)
        {

        }

        void Current_Deactivated(object sender, Microsoft.Phone.Shell.DeactivatedEventArgs e)
        {

        }
#endif

 

Now, the second thing that you’ll have to do is to change the build configuration to prevent Visual Studio deploying your game every time. If you don’t do this the sequence goes like this:

Run Game > Press Start  (Game terminates in background) > Press Back (blank screen displayed) > Press F5/Run in Visual Studio (this builds, deploys and attempts to run game) > Game runs as if it wasn’t tombstoned (ie Launching event raised instead of Activated)

To get the Activated event to fire you need to adjust the build configuration so that it doesn’t deploy the game every time you press F5/Run. Right-click on the solution in Solution Explorer and select Configuration Manager. Uncheck the box in the Deploy column.

image

You can still deploy your XNA game to the device, you just need to right-click on the project and select Deploy (remember to do this each time you make changes to your XNA game to make sure you are debugging the latest version).

Run Game > Press Start  (Game terminates in background) > Press Back (blank screen displayed) > Press F5/Run in Visual Studio (runs game) > Game runs as if it were tombstoned (ie Activated event raised)

Leave a comment