Take Away’s:
- Do not use the built in ProgressBar straight up, use Jeff’s template
- When you’re done with an indeterminate ProgressBar, make sure to toggle IsIndeterminate to False and Collapse the bar
- General: Always make sure to stop animations / remove animating controls when they’re no longer needed
Some Background:
Due to a bunch of different reasons the shipping ProgressBar control is suboptimal and will actually be UI thread bound – meaning that if your UI thread is stuck working, your ProgressBar will be stuck as well. Not a great situation for a ProgressBar, huh?
That said, we’re not leaving you high and dry. Jeff Wilcox has a great solution which changes the template for the ProgressBar to only run on the Render thread – meaning that it will continue ticking, even when you’re doing your heavy loading work on the UI thread. That said, it still comes with a gotcha – don’t forget to set IsIndeterminate to False and to Collapse the bar once your done (instead of just setting Visibility to Hidden) so that the ProgressBar doesn’t continue to tick in the background, eating up Render thread cycles.
As a general rule, highlighted especially by the ProgressBar, you should always make sure to stop animations (not pause) and remove / collapse animating controls when they’re no longer needed.
Remember: just because you can’t see a animation / control doesn’t mean that it isn’t there doing work.
I’m kicking off a series of posts about Silverlight perfofmance under Windows Phone 7 with a a kind of obvious one, but one that is important to keep in mind from the get go.
Tip:
- Test your code on device as much as possible
But the Emulator is awesome?!?
True, the emulator, otherwise known as the x86 Device Emulator, or simply XDE, is awesome, but it is still not an accurate representation of a device. In fact since the XDE is usually so smooth, it’s extremely easy to fall into the trap of adding more features “because it works on the emulator”.
The Hardware
The emulator restricts itself to one core, adds artificial Sleep()’s and limits the amount of memory it is happy to eat up (so it won’t just chew through whatever is available), but that still isn’t enough.Chances are that even running at one core the emulator is still running faster than the device (most cores today are going to be running faster than 1GHz and chances are you are going to have less things running on that core than the device does). Throw in a desktop GPU which beat a mobile GPU handsdown and you’ve got a winning combination. If you happen to have an older machine, then the XDE will simply run like a dog – and you won’t be able to tell if your app crawls because of your code or because of your machine.
But I don’t have a device!
Common problem, especially in these trying, pre-release, times. Fear not though! Your local Microsoft office most likely has some devices and can help hook you up. Shoot them an email and let them know that you are working on an app, include a description and some screenshots from the XDE (to sweeten the deal) and they should be able to help.
There’s a change in the pipeline that will be hitting the public Windows Phone 7 images at some point soon (post the current Beta), which removes the ScrollViewer from a TextBox’s template.
What does this mean?
Basically, long TextBoxes will no longer scroll when you gesture over them – the gesture is ignored and there is no visual reaction. Tapping remains the same (the keyboard pops up) as does tapping and holding (the enlarged caret is shown, and you can use this to, sort of, scroll).
How do I work around this?
Most scenarios do not require scrollable TextBoxes, but if your’s does you can either place the TextBox inside a ScrollViewer (though this can cause some texture issues), or preferably re-add the ScrollViewer into your TextBoxes template, so that it still scrolls as usual.
Original Post
Here’s a small tip for those of you who want to debug performance in a Windows Phone Silverlight app with the frame rate counters, but have the System Tray visible – hide it.
The counters currently show up behind the system tray (since technically the tray is a system overlay which is drawing over the surface available to your Silverlight app), so hiding the tray will show the counters.
Don’t forget: to re-enable the system tray when you’re done!
Here’s an awesome gotcha when moving from a desktop Silverlight application to a Windows Phone 7 application – make sure that your media (wmv) files are set to “Build Action” = “Content” and not “Resource”.
You’ll notice that if you do something like:
<MediaElement Source="somevideo.wmv"/>
Where “somevideo.wmv” is set to “Content”, then the Windows Phone Developer Tools (ie. Visual Studio) will underline the "Source" attribute and recommend that you set it to “Resource”. This is a hangover from the desktop and is something that I hope will go away – you can safely ignore this warning (it won’t appear in your build windows).
What’s Wrong With “Resource”?
For those that want more, here are the potential problems you can run into when setting your media to “Resource”:
- When a video file is compiled as a Resource it incurs an extra space and performance hit every time you play it, since Silverlight does extra processing to extract the video from your assembly (DLL). In the case of “Content” the file can be read directly from disk (or memory) and you’ll get instant start playback.
- Anything that makes your DLL larger is evil (from my point of view) – you want your assemblies to be small (think “quick and nimble”). Although the size doesn’t always directly affect load and memory time (there are a couple of other factors at play here) this helps eliminate one more possible bottleneck.