The Art Of Silverlight

Going beyond rich applications… with Miroslav Miroslavov

Silverlight’s community in Bulgaria is getting stronger every day, due to a couple of reasons:

  • The course we’re having at The University of Sofia. Currently we have more than 100 students eager to know and start using Silverlight. Till now we had 5 very good lectures and nice demos in Bulgarian language.
  • The very strong and permanent Sofia’s Silverlight group. We meet every month to share and learn “What’s new” and “How to”  with Silverlight.

You can also meet the Bulgaria’s Silverlight MVP – Emil Stoychev at MicrosoftFeed.

This list with performance tips is collected from many sources, including my personal experience. It is supposed to evolve and grow in the future.

Basic performance tips, regarding your code

  • Minimize your XAML – the smaller the better. Do not allow Blend or VS to blow it up.
  • Always think about “The Visual Tree”. Keep it small. Understand all controls with their visual trees. Use the simplest controls that will work in your case.
  • Always collapse not needed UIElements or remove them from the Visual Tree.
  • Opacity = 0 is different from Visibility = Collapsed.
  • Use Image.Stretch = “Fill” to prevent any layout updates due to stretching, whenever is possible.
  • Do NOT use Path with explicit Height/Width values and do NOT allow parent controls to stretch it. Paths are supposed to be as big as the Data property says (Unless you don’t need it).

Silverlight Plug-in options

  • Avoid Using Windowless Mode
  • Use Transparent Background only when needed

Animations

  • Always be aware of the frame-rate.
    • Think about the frame-rate on per Animation bases. More about this here.
  • Big moving elements are always CPU expensive (Due to big redrawn regions).
  • Understand each animation how expensive is.
  • Understand CompositionTarget.Rendering event and use it if possible.
  • Storyboards and animations over big regions are very expensive. Try to minimize them.
  • Do not allow layout pass while animation is running (unless this is what you need). This means not to animate properties that are related to the Layout system like Height, Width, Visibility, Alignments, Margins…
  • Text Animations – By default, Silverlight optimizes text for readability. That’s why animating text is expensive. Unless we do one of the following.
    • Set TextRenderingMode = RenderForAnimation
    • If the text is static, we can replace it with Image of the text and animate the Image control.

Monitor the performance during development

  • MaxFrameRate = 10.000
  • EnableFrameRateCounter = true
  • EnableRedrawRegions = true
  • Use Process Explorer to monitor the CPU usage

Advanced tips

UI Caching

  • WriteableBitmap – more on this here.

  • GPU cache – more on this here.

Can be very useful when cached elements are being translated, scaled, rotated or blended using opacity. Also it will work better if the cached elements are Leaf nodes in the visual tree.

  • Limitations: You can’t cache elements that have been:
    • Projected using Plane/MatrixProjections
    • Applied effect, including Pixel Shader.
    • Applied opacity mask.
    • Applied non-rectangular clipping.

In order to make this list more helpful – all comments are more than welcome.