Tips and tricks for building windows phone 7 applications and games

Now that I have finished the first version of my Mahjong Solitaire game, it’s time to share some things I’ve learned. Designing for the Windows Phone is very different from ASP.NET or Silverlight Apps. You have to think about tombstoning, try/buy-licences, marketplace submission and all kinds of stuff you won’t find in other online applications. This is not a tutorial or code explanation. It’s a list of things you will encounter while designing for WP7.

1. Tombstoning (deactivate/activate)

When your application is closed (Back-button pressed, Call coming in) it is not running in the background. It’s not running at all. You need to save data when this happens. Use Application_Launching, Application_Activated, Application_Deactivated and Application_Closing to serialize and deserialize your data. I’ve used the ApplicationSettings for this:

IsolatedStorageSettings.ApplicationSettings.Add(key, data);

My advice is to copy view model properties to a GameStateData object and serialize this object when the application is deactivated. When the application activates, you can read from the ApplicationSettings and restore the data to the view model. Restore data to the viewmodel in the App.xaml.cs! When Mainpage.OnNavigatedTo fires, your application should have a valid viewmodel and does not need to know if the viewmodel is new or restored. If you added dynamically created UIElements, you need to redraw them after your application is re-activated.

Tombstoning is something you should do at the start of your project. Adding it later forces you to test everything all over.

2. Licence (trial/payed)

One other thing you should implement from the start, is a Licence Manager. You can write one or google them online. In Mahjong I am interested in three posibilities:

public enum LicenceResult
    {
        Unlocked = 0,
        Trial,
        TrialExpired,
    }

I need this later for tracking events. Use the LicenseInformation class to find out if your application is a trial version:

private LicenseInformation _licenseInformation = new LicenseInformation();

if (_licenseInformation.IsTrial())
{

3. Tracking events

You will be amazed what you learn when you collect event-data from your application. I was interested in how many games were started, won, replayed. But you can also track if exceptions and other unwanted behavior occurs. The exception-details (stack-trace) is not logged. I’ll explain how to do this in the next chapter. To set up event tracking, follow these steps:

In Mahjong Solitaire I never track personal information. I would not like it if my information was collected without my knowledge!

4. Error reports

If an unhandled exception occurred, I let the user choose if he or she want’s to inform me about it. I use Andy Pennell’s excellent LittleWatson for this. It uses the EMail Launcher so a user is always aware that information is being sent to you.

5. Graphics

I have designed all my graphics in Expression Design which uses vector graphics so I can export them in all sizes. That is very handy for the application and marketplace icons.

mahjong

The crispy graphics you see in Mahjong Solitaire are the direct result of the great rendering engine of Expression Design. I have exported everything to PNG.

Test your project often with different windows phone color schemes!

Testing & Submitting to Marketplace

Take testing serious. Null-references, unhandled exceptions…test….test…test….and test more. Ask anyone to play your game or use your app. Look how they use it and check for errors.

When you are sure there are no errors in your version (but there will be!) submit it to the Marketplace. My advice is to type all the info that is asked by the APP HUB in a text file and put it in your project. You can re-use the info for future submissions. Have images ready. Marketplace needs a 200×200, 173×173, 99×99 icon and at least one screenshot.

Don’t show the phone emulator chrome in the screenshot!

Conclusion

Designing a Windows Phone game or application is fun and can be a great challenge. You have to be very comfortable with the fact you are going to ship “Work in progress”. Your first application might take a few versions before everything is stable. It made me wonder: can you image how many test hours the Nintendo team spent before shipping The Legend of Zelda: Ocarina of time?