BitmapEffects

Andrew Smith [Infragistics] / Wednesday, April 9, 2008

BitmapEffects should be used sparingly since currently1 they are software rendered but judicious limited usage of them can provide some nice effects for your application. In some cases, their usage is necessary to duplicate a particular look and feel - e.g. using a OuterGlowBitmapEffect to mimic the glow around text in the title area of the ribbon window. However, BitmapEffects are not allowed when running in a limited trust situation - e.g. xbap - so if your template defines a BitmapEffect, that template cannot be used when running in an xbap.

There are several options you could choose. You could create the bitmap effect in code so you can catch any security exceptions that might arise. This option allows you to catch the exception that would be generated when you don't have the security required to create it but it assumes that the developer knows when the bitmap effect will be needed. If you're working with a designer, you'll want to leave it up to them. The look of the element should be kept as separated as possible from the behavior/code behind. Another option is to create two templates and decide in a style trigger which to use depending on whether you're running in an xbap. This too isn't a great option since it means you have to maintain two different templates - more if you have to re-template the element for different themes.

We chose another route - we created the SafeOuterGlowExtension class. This class is a markup extension that can be used to define an OuterGlowBitmapEffect and exposes the same properties available on that class. Since the OuterGlowBitmapEffect is not defined in the xaml, the template can be used even when running in an xbap, in which case the bitmap effect is not created. Currently, this is the only bitmap effect exposed in this manner since it was the only one we needed at the time. If you need to use another bitmap effect, you could follow a similar route for the other bitmap effect classes.

Example usage:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:igWindows="http://infragistics.com/Windows"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
  <TextBlock Text="This text has a glow"
      BitmapEffect="{igWindows:SafeOuterGlow GlowColor=Yellow}"/>
</Page>

Notes
1. Some of the bitmap effects will be hardware accelerated in a service release due later this year.