Live Chat Icon For mobile
Live Chat Icon

WPF FAQ - Freezable Object

Find answers for the most frequently asked questions
Expand All Collapse All

This example shows how to determine whether a Freezable object is frozen. If you try to modify a frozen Freezable object, it throws an ’Invalid Operation’ Exception. To avoid throwing this exception, use the ‘IsFrozen’ property of the Freezable object to determine whether it is frozen.

[C#]

Button myButton = new Button();
SolidColorBrush myBrush = new SolidColorBrush(Colors.Yellow);

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

if (myBrush.IsFrozen) // Evaluates to true.
{
    // If the brush is frozen, create a clone and
    // modify the clone.
    SolidColorBrush myBrushClone = myBrush.Clone();
    myBrushClone.Color = Colors.Red;
    myButton.Background = myBrushClone;
}
else
{
    // If the brush is not frozen,
    // it can be modified directly.  
    myBrush.Color = Colors.Red;
Permalink

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, and it can no longer be modified. It also provides a changed event to notify observers of any modifications to the object. it improves 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 be shared across threads. The Freezable class makes it easier to use certain graphics system objects and can help improve application performance.

To freeze a Freezable object declared in markup, you can use the PresentationOptions ‘Freeze’ attribute.

In the following example, a ’SolidColorBrush’ is declared as a page resource and frozen. It is then used to set the background of a button.

[XAML]

<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/presentation/options' 
  xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006'
  mc:Ignorable='PresentationOptions'>
  <Page.Resources>
    <!-- This resource is frozen. -->
    <SolidColorBrush 
      x:Key='MyBrush'
      PresentationOptions:Freeze='True' 
      Color='Red' />
  </Page.Resources>
  <StackPanel>
    <Button Content='A Button' 
      Background='{StaticResource MyBrush}'>
    </Button>
  </StackPanel>
</Page>
Permalink

This example shows how to use the Clone method to create a writable copy of a read-only Freezable. After a Freezable object is marked as read-only (‘frozen’), you cannot modify it. However, you can use the ’Clone’ method to create a modifiable clone of the frozen object.

The following example creates a modifiable clone of a frozen ’SolidColorBrush’ object.

[C#]
Button myButton = new Button();
SolidColorBrush myBrush = new SolidColorBrush(Colors.Yellow);
[C#]
// Freezing a Freezable before it provides
// performance improvements if you don’t
// intend on modifying it. 
if (myBrush.CanFreeze)
{
    // Makes the brush unmodifiable.
    myBrush.Freeze();
}

myButton.Background = myBrush;  

// If you need to modify a frozen brush,
// the Clone method can be used to
// create a modifiable copy.
SolidColorBrush myBrushClone = myBrush.Clone();

// Changing myBrushClone does not change
// the color of myButton, because its
// background is still set by myBrush.
myBrushClone.Color = Colors.Red;

// Replacing myBrush with myBrushClone
// makes the button change to red.
myButton.Background = myBrushClone;
Permalink

Share with

Couldn't find the FAQs you're looking for?

Please submit your question and answer.