A Freezable is a type of object with two states: frozen or unfrozen. When unfrozen, a Freezable behaves like any other object. When frozen, it can no longer be modified and becomes immutable. Although the Freezable class has many applications, most Freezable objects in Windows Presentation Foundation (WPF) are related to the graphics subsystem. For example, Brush, Transform, and Geometry all inherit from Freezable.
The Freezable class can improve application performance. Freezable objects often hold unmanaged resources that need to be monitored. Freezing an object improves performance because the runtime no longer needs to track change notifications (the Changed event). In addition, a frozen object can be shared across multiple threads, which is not possible for an unfrozen one.
#How to freeze an object in code?
C#
SolidColorBrush myBrush = new SolidColorBrush(Colors.Yellow);
myBrush.Freeze();
#How to freeze an object from XAML?
To freeze an object in XAML, add the XML namespace http://schemas.microsoft.com/winfx/2006/xaml/presentation/options.
XAML
<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!