- By Brian Rasmussen
Making the key scenarios of your app perform well is paramount for a good user experience and positive ratings on the Windows Store. Unless your app accurately predicts tomorrow’s stock prices, users don’t want to wait for your app to respond. Of course, if your app can predict the market, I’m pretty sure most users will accept a little waiting and you can ignore most of the advice in this chapter. For the rest of us, here’s a summary of what I covered.
Designing for performance means getting rid of everything that isn’t essential for the experience. If it isn’t needed, don’t do it. If it’s needed later, do it later. If it takes a long time to do, save the result.
You need to identify the key scenarios and the resources needed to implement those. Once you identify the resources, you need to prioritize them so that your app can retrieve and handle the most important assets first. Your goal should be to optimize for responsiveness. Make sure the user can interact with the important parts of your app with as little delay as possible. Everything else can be handled once the app is responsive.
If your app needs to retrieve data from the network, you need to address the fact that you cannot guarantee a fast and steady rate of data. If your app is not useful without a fast network connection, you’re cutting off a lot of users. You need to design your app so that it handles poor network connectivity. Windows offers several tools to help you do that.
The ContentPrefetcher class introduced in Windows 8.1 lets your app subscribe to automatic content updates even when it isn’t running. This reduces the risk of cached data being stale. Launching with local content is the only way to guarantee a specific performance.
Asynchronous I/O lets your app retrieve data without blocking the UI thread. This keeps your app responsive, but it doesn’t change the fact that I/O might take a long time, asynchronous or not. You need to design around this. If your app retrieves data as part of launch or navigation, you need to provide a fallback option in the case of a slow network connection. The WindowsRuntimeSystemExtensions class provides a series of extension methods that bridges the gap between the WinRT APIs and the .NET Task Parallel Library. This means you can wait for and cancel asynchronous calls using the familiar mechanisms of the .NET library.
A lot of work went into making Windows 8.1 even better at presenting large datasets to the user efficiently. The grid and list controls were both updated to support faster virtualization. If you’re building for 8.1, you get these benefits automatically, but if you’re upgrading your Windows 8.0 app, you should make sure your grids use the ItemsWrapGrid panel. This panel supports item-level virtualization, which improves performance significantly for large groups in a grid. Furthermore, it allows you to display generic placeholders for items that are still being rendered. To improve the experience beyond that, you can even implement your own placeholders, which allows you to render each element in phases.
Media content is an integral part of many Windows apps. If your app handles images, video, or audio, you can do several things to improve the experience. The most important part is to make sure you’re not forcing XAML to do more work than necessary for each item. For images and video, that means scaling resolution as appropriate. For videos, that means deferring as much work as possible and preferring full-screen playback over embedded playback. Because media content is demanding on the system, your app needs to release resources as soon as they are no longer needed.
To help you measure the performance of your code and ensure the techniques discussed in this chapter are working in your favor, you should instrument your code, and doing so is the topic of Chapter 4, “Instrumentation.”