Freezable objects
A Freezable is a type of object that has two states: frozen or unfrozen. When it is not frozen, a Freezable object behave like any other object. However, when it is frozen, a Freezable object can no longer be modified (immutable). Although the Freezable class has many applications, most Freezable objects in Windows Presentation Foundation (WPF) are linked to the graphics subsystem. For instance Brush
, Transform
and Geometry
inherits from Freezable
.
The Freezable class can improve the performance of the application. Indeed, freezable object often contains unmanaged resources which may need to be monitored. Freezing an object can improve its performance because it no longer needs to devote resources to change notifications (Changed
event). In addition, a frozen object can also be shared among several threads, which is not the case for an unfrozen object.
#How to freeze an object from the code?
SolidColorBrush myBrush = new SolidColorBrush(Colors.Yellow);
myBrush.Freeze();
#How to freeze an object from XAML?
To freeze an object in XAML, you must add the xml namespace http://schemas.microsoft.com/winfx/2006/xaml/presentation/options
.
<SolidColorBrush
x:Key="MyBrush"
xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
PresentationOptions:Freeze="True"
Color="Red">
Do you have a question or a suggestion about this post? Contact me!