Freezable Objects Overview
Freezable Objects Overview
03/30/2017
9 minutes to read
o
What Is a Freezable?
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.
The Freezable class makes it easier to use certain graphics system objects and can
help improve application performance. Examples of types that inherit
from Freezable include the Brush, Transform, and Geometry classes. Because they
contain unmanaged resources, the system must monitor these objects for
modifications, and then update their corresponding unmanaged resources when
there is a change to the original object. Even if you don't actually modify a
graphics system object, the system must still spend some of its resources
monitoring the object, in case you do change it.
For example, suppose you create a SolidColorBrush brush and use it to paint the
background of a button.
C#Copy
Button myButton = new Button();
SolidColorBrush myBrush = new SolidColorBrush(Colors.Yellow);
myButton.Background = myBrush;
When the button is rendered, the WPF graphics sub-system uses the information
you provided to paint a group of pixels to create the appearance of a button.
Although you used a solid color brush to describe how the button should be
painted, your solid color brush doesn't actually do the painting. The graphics
system generates fast, low-level objects for the button and the brush, and it is
those objects that actually appear on the screen.
If you were to modify the brush, those low-level objects would have to be
regenerated. The freezable class is what gives a brush the ability to find its
corresponding generated, low-level objects and to update them when it changes.
When this ability is enabled, the brush is said to be "unfrozen."
Note
C#Copy
if (myBrush.CanFreeze)
{
// Makes the brush unmodifiable.
myBrush.Freeze();
}
Note
For convenience, freezable objects remain unfrozen unless you explicitly freeze
them.
Using Freezables
Using an unfrozen freezable is like using any other type of object. In the following
example, the color of a SolidColorBrush is changed from yellow to red after it's
used to paint the background of a button. The graphics system works behind the
scenes to automatically change the button from yellow to red the next time the
screen is refreshed.
C#Copy
Button myButton = new Button();
SolidColorBrush myBrush = new SolidColorBrush(Colors.Yellow);
myButton.Background = myBrush;
Freezing a Freezable
If these conditions are false, and you don't intend to modify the Freezable, then
you should freeze it to gain the performance benefits described earlier.
if (myBrush.CanFreeze)
{
// Makes the brush unmodifiable.
myBrush.Freeze();
}
myButton.Background = myBrush;
try {
C#Copy
if (myBrush.CanFreeze)
{
// Makes the brush unmodifiable.
myBrush.Freeze();
}
myButton.Background = myBrush;
In the preceding code example, a modifiable copy was made of a frozen object
using the Clone method. The next section discusses cloning in more detail.
Note
XAMLCopy
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presen
tation/options"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="PresentationOptions">
<Page.Resources>
<StackPanel>
</StackPanel>
</Page>
XAMLCopy
xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presen
tation/options"
Because not all XAML readers recognize this attribute, it's recommended that you
use the mc:Ignorable Attribute to mark the Presentation:Freeze attribute as
ignorable:
XAMLCopy
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="PresentationOptions"
"Unfreezing" a Freezable
In the following example, the button's background is set with a brush and that
brush is then frozen. An unfrozen copy is made of the brush using
the Clone method. The clone is modified and used to change the button's
background from yellow to red.
C#Copy
Button myButton = new Button();
SolidColorBrush myBrush = new SolidColorBrush(Colors.Yellow);
myButton.Background = myBrush;
Regardless of which clone method you use, animations are never copied to the
new Freezable.
If your class contains non-dependency property data members, you must also
override the following methods:
CloneCore
CloneCurrentValueCore
GetAsFrozenCore
GetCurrentValueAsFrozenCore
FreezeCore
You must also observe the following rules for accessing and writing to data
members that are not dependency properties: