Click here to Skip to main content
15,885,842 members
Articles / Desktop Programming / WPF
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.73/5 (4 votes)
12 Apr 2010CPOL1 min read 18.3K   3  
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer http://wpf-france.fr
France (Metropolitan) France (Metropolitan)
Jonathan creates software, mostly with C#,WPF and XAML.

He really likes to works on every Natural User Interfaces(NUI : multitouch, touchless, etc...) issues.



He is awarded Microsoft MVP in the "Client Application Development" section since 2011.


You can check out his WPF/C#/NUI/3D blog http://www.jonathanantoine.com.

He is also the creator of the WPF French community web site : http://wpf-france.fr.

Here is some videos of the projects he has already work on :

Comments and Discussions

 
-- There are no messages in this forum --