November 17th, 2009

When building Silverlight applications, especially ones with multiple animations and storyboards, it is easy to quickly find your processor requirements start spiking. This can reduce your viewers experience through slow frame rates and jumpiness, especially on lower spec machines.

One way to optimize your Silverlight application is to check that it is not having to render objects that are not being used or seen. If a storyboard is animating an object then that object is redrawn each frame even if it is out of view or behind another object. By stopping these animations or collapsing them until we need them will help keep down the processor requirements.

By using the ‘EnableRedrawRegions‘ property we can see every part of the screen that is being redrawn each frame.

Below is an example of an animation and the ability to move an object on top to hide it.

Please upgrade your browser

Below is the same example but with the EnableRedrawRegions property switched on. Notice how the animation is still being redrawn even when it is fully hidden by the Black area, but if we press the Collapse button the animation is no longer redrawn.

Please upgrade your browser

The ‘how to’ bit

To turn on the EnableRedrawRegions property we need to add the following code in the HTML page hosting the Silverlight:

<param name=“enableRedrawRegions” value=“true” />

Add it after the ‘object‘ tag with the other default ‘param‘ tags created by Visual Studio.

Blend 3 allows you to embed fonts, but unfortunately if you publish your work online you are technically distributing the font. This is illegal and could potentially land you in trouble.

However, there is a way around this, if you encrypt the font so that it is not usable then you are arguably not distributing it any more. This process is known as Obfuscation. It is important, however, to point out that you still need a license or permission to use a non-standard font in your work.

How to Obfuscate a font

First open up Microsoft Word (2007+), create a new document and type every character you will need in the font you require. Save the file as an XPS document and close Microsoft Word.

Locate the saved XPS document on your hardrive and rename the file type from ‘.xps‘ to ‘.zip‘. Now extract the contents of the .zip and find the file called ‘Resources‘, inside this folder should be a document with the file type ‘.odttf‘. Add this file into your Silverlight Solution and set its ‘Build Action‘ to ‘Resource‘.

Now the final part, in the XAML, locate your textBlock and set the ‘FontFamily‘ property to the .odttf file in your solution. Directly after this needs to be a ‘#‘ and then the exact name of how the font was displayed in the selection field in Microsoft Word. This allows you to have multiple fonts in one .odttf file.

If you are using multiple instances of elements then it may make more sense to think about making these elements User Controls. User Controls allow you to place objects into a container that can be re-used again and again, even multiple times at once. Every User Control has its own XAML file and its own code file.

To make a selection of elements on stage into a User Control it is really simple. First select all the objects you want to include in your new User Control, then right click anywhere on the selected objects. From the drop down options select ‘Make into UserControl…‘. This creates a new XAML file containing the objects you selected with it’s own code behind file too.

Each time you want to add a new instance of your User Control you will need to add the following XAML in…