Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Freeze brushes directly in the XAML to improve your application's performances

0.00/5 (No votes)
12 Apr 2010 1  
When you read the MSDN guidelines to improce WPF's performances you can find that it's a good idea to freeze Freezable objects.It's a quite easy thing to do via the code but it's quite harder to do it directly in the XAML. In this post we will see how to do so.What are freezable...
When you read the MSDN guidelines to improce WPF's performances you can find that it's a good idea to freeze Freezable objects.

It's a quite easy thing to do via the code but it's quite harder to do it directly in the XAML. In this post we will see how to do so.


What are freezable objects ?


One upon a time, the MSDN said :

A Freezable is a special type of object that has two states: unfrozen and frozen. When unfrozen, a Freezable appears to behave like any other object. When frozen, a Freezable can no longer be modified.

A Freezable provides a Changed event to notify observers of any modifications to the object. Freezing a Freezable can improve its performance, because it no longer needs to spend resources on change notifications. A frozen Freezable can also be shared across threads, while an unfrozen Freezable cannot.




With this definition in mind, you surely had guess that it's a good thing to freeze them because and you surely want to do it... But how to do ?

Freeze freezable objects via the code :

It's quite easy to do it in the code. The only thing do check is the property CanFreeze which tells you if you can or not freeze the Freezable object. You will then use this code :

if (myBrush.CanFreeze)
{
    // Makes the brush unmodifiable.
    myBrush.Freeze();
}




Freeze them (with ice) in XAML :

The tips is to know the good XML Namespace http://schemas.microsoft.com/winfx/2006/xaml/presentation/options.

The use in the XAML is then quite easy :
<LinearGradientBrush ice:Freeze="True" 
xmlns:ice="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options" 
/>


(originaly posted on http://blog.lexique-du-net.com/index.php?post/2010/04/12/Freeze-brushes-directly-in-the-XAML-to-improve-your-application-s-performances[^]

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here