Loading Optimizations - How I optimized my loading time with almost 3 seconds

The initial loading time of a application is important, every Windows Phone developer know this. Besides the time-limit requirement for marketplace certification, it's also about providing the fastest and smoothest experience to the user. And here, the initial loading time of the application is very important. Large apps like Facebook or Twitter (official client), are extremly slow at starting, and this really disencourages the use of the application. Likewise, slow startup apps requires a splash screen, something that isn't entirely native to the Windows Phone Experience.

So the last couple of weeks I've been updating my applications in preparation for Mango. And as part of these updates, I also did a good piece of work on the loading speed on my most popular application, DMI City Weather (a weather app for Denmark). And here's how I did it.

1. Delayed the loading of the ViewModel

A lot of people, including myself, tends to load the viewmodel either directly in XAML, or using a ViewModelLocator from XAML. Both of this means that the ViewModel is created as part of the view class, and such delays loading the view into memory. So to delay the loading of the viewmodel, I moved the initialization code into the OnNavigatedTo event, like this:

Notice that I embedded all loading code in the dispatcher, to ensure the code is first executed when the UI thread is idle, meaning that all initial UI loading is done. I'm also using the SmartDispatcher, to simply the use of the dispatcher.

2. Lazy-load Pivot items

For my initial view, I use a rather large pivot (7 pivot pages). If loaded directly this means all pivots are being loaded at once (though incrementally), and so it lags the UI thread while loading. So instead, I seperate each pivot to a UserControl, and then use the LoadingPivotItem event to initialize each view, and setting the datacontext of each UserControl to the ViewModel for the entire view. (Thanks to Andrej Torzon for the tip!)

3. Use the dispatcher for everything

I can't stress this point enough. Use the dispatcher for all operations, specially navigation, to ensure the actions are queued on the UI thread, rather than invoked directly. This gives a much smoother experience for the user. And while the impact might be bigger for the entry view, using it on all views ensures that all transition effects, or the likes are executed properly, without any lag.

4. Removed SplashScreenImage.jpg

For fast loading applications (below 2 seconds) the splash screen adds additional visual delay to the loading of the application

The results of the optimization was a loading time improvement on just about 3 seconds. Now the application loads on just about half a second, and no longer needs a splash screen.