Wednesday, 6 November 2013

Windows Phone Development Tips

For the past several months I have been knee-deep in developing a commercial Windows Phone 8 app for a well-known sports company (NDA prevents me from disclosing any specifics).  While this is my seventh Windows Phone app, there are always new things to discover.  Here are a few tips I would like to impart, which you may or may not find useful.


WP8 developers can take advantage of the new async and await keywords in C#.  If you're unfamiliar with asynchronous programing here's a good place to start.  For both Windows Phone and WinRT applications, responsiveness is no longer a "nice to have", users expect your application to instantly respond to user input.  The Modern UI is fast, fluid and very responsive.  async/await is your best friend in making that happen. 

Monitor Memory Consumption

When embarking on my latest WP project I was fortunate to have a team of UI/UX designers.  They had spent a lot of time creating wireframes and screen mockups.  They had created some very rich screens, loaded with great content and functionality.  As a developer I immediately go into design mode, figuring out how to build the screens, behaviours and functionality.  Quite quickly my app came together, as well as the complexities.  It's easy to get caught up in the moment and forget about things like memory management.  So, I had a fully functioning app that worked great in the emulator and on my Lumia 920, but sucked on low/mid-range devices. 

Add these lines of code to your pages:

public MyPage()
     Debug.WriteLine(string.Format("MyPage constructor {0}", DeviceStatus.ApplicationCurrentMemoryUsage));

     Debug.WriteLine(string.Format("MyPage destructor {0}", DeviceStatus.ApplicationCurrentMemoryUsage));

 This will allow you to monitor your apps memory consumption and identify pages that are not being destroyed.  You can do the same with your ViewModels, if you are using MVVM.

Additionally, you can periodically run the Windows Phone Application Analysis Tool (Debug | Start Windows Phone Application Analysis) to locate troublesome areas of your app.

Use WVGA 512MB Emulator...and buy a Lumia 520

When working in the development environment, use the WVGA 512MB Emulator.  While it is not exactly the same as a 512MB device, it will give you a feel for lower end devices.

If you're serious about Windows Phone development do yourself a favour and pickup a Lumia 520 as a test device.  The 520 is one of the most popular Windows Phone on the market, and for about £100 (SIM-free) its a steal.  Sideload your app onto your 520 and give it whirl.  If your app behaves well on the 520 you can pretty guarantee it will perform on other devices.  I only recently picked up a 520 in an emergency after my 920 died and was returned to Nokia.  Running your app on something like a 920 doesn't tell you much - of course its going to kick-ass, its a kick-ass phone.

Clean-up after yourself

Because your are working with limited memory (150MB for low end device, 300MB for high end), for larger apps you eventually become aware of this ceiling.  It is a good practice to tidy-up after yourself once the user is completed with a page.  I generally do this on the BackKeyPress event, but it could also be done in the OnNavigatedFrom override, depending on your circumstances.

If you're using MvvmLight you can call the CleanUp method on your ViewModel to decouple EventHandlers, dispose of unused objects and resources, etc.

Additionally, the issue regarding images on Windows Phone is well-known.  So, this is a good place to purge any unused images.

Give your app to a kid

By chance I let my 14 year old use my app. was revealing.  Granted, he's a pretty switched-on kid, but observing him do things with the app (or try and do things) was a real eye-opener.  As well, I had him talk about the app as he was using it, telling me what he liked or didn't like, what was intuitive, what was confusing.  This resulted in some major changes in the user journeys we had originally designed.

Okay, a 14 year old is not a focus group.  But, on a budget, it was free and provided invaluable feedback.

Use IsolatedStorage, not Windows.Storage

I started out using IsolatedStorage, then mid-development switched to Windows.Storage - big mistake.  Stefan Wexel writes about this on StackOverflow, check it out:

StorageFile 50 times slower than IsolatedStorageFile

I'm sure Windows.Storage will improve in time, but for now stick with IsolatedStorage for Windows Phone 8.

Well, that's it for now;  I hope these tips were of some use.  Will post more as they hit me in the 'ead.

No comments: