Export (0) Print
Expand All
2 out of 3 rated this helpful - Rate this topic

Freezable Class

Defines an object that has a modifiable state and a read-only (frozen) state. Classes that derive from Freezable provide detailed change notification, can be made immutable, and can clone themselves.

Namespace:  System.Windows
Assembly:  WindowsBase (in WindowsBase.dll)
public abstract class Freezable : DependencyObject
This class is abstract; see Inheritance Hierarchy for derived non-abstract classes usable in XAML.

The Freezable class provides special features that can help improve application performance when using objects that are expensive to modify or copy. Examples of Freezable objects include the following:

Deriving From Freezable

A class that derives from Freezable gains the following features:

  • Special states: a read-only (frozen) state and a writable state.

  • Thread safety: a frozen Freezable object can be shared across threads.

  • Detailed change notification: Unlike other DependencyObject objects, a Freezable object provides change notifications when sub-property values change.

  • Easy cloning: the Freezable class has already implemented several methods that produce deep clones.

For information on using and creating your own Freezable objects, see Freezable Objects Overview.

This example shows how to make a Freezable read-only by calling its Freeze method.

You cannot freeze a Freezable object if any one of the following conditions is true about the object:

  • It has animated or data bound properties.

  • It has properties that are set by a dynamic resource. For more information about dynamic resources, see the Resources Overview.

  • It contains Freezable sub-objects that cannot be frozen.

If these conditions are false for your Freezable object and you do not intend to modify it, consider freezing it to gain performance benefits.

The following example freezes a SolidColorBrush, which is a type of Freezable object.

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

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

myButton.Background = myBrush;  

For more information about Freezable objects, see the Freezable Objects Overview.

More Code

How to: Obtain a Writable Copy of a Read-Only Freezable This example shows how to use the Clone method to create a writable copy of a read-only Freezable.
How to: Determine Whether a Freezable Is Frozen This example shows how to determine whether a Freezable object is frozen. If you try to modify a frozen Freezable object, it throws an InvalidOperationException. To avoid throwing this exception, use the IsFrozen property of the Freezable object to determine whether it is frozen.
System.Object
  System.Windows.Threading.DispatcherObject
    System.Windows.DependencyObject
      System.Windows.Freezable
        System.Windows.Media.Animation.Animatable
        System.Windows.Media.Animation.BooleanKeyFrame
        System.Windows.Media.Animation.BooleanKeyFrameCollection
        System.Windows.Media.Animation.ByteKeyFrame
        System.Windows.Media.Animation.ByteKeyFrameCollection
        System.Windows.Media.Animation.CharKeyFrame
        System.Windows.Media.Animation.CharKeyFrameCollection
        System.Windows.Media.Animation.ColorKeyFrame
        System.Windows.Media.Animation.ColorKeyFrameCollection
        System.Windows.Media.Animation.DecimalKeyFrame
        System.Windows.Media.Animation.DecimalKeyFrameCollection
        System.Windows.Media.Animation.DoubleKeyFrame
        System.Windows.Media.Animation.DoubleKeyFrameCollection
        System.Windows.Media.Animation.Int16KeyFrame
        System.Windows.Media.Animation.Int16KeyFrameCollection
        System.Windows.Media.Animation.Int32KeyFrame
        System.Windows.Media.Animation.Int32KeyFrameCollection
        System.Windows.Media.Animation.Int64KeyFrame
        System.Windows.Media.Animation.Int64KeyFrameCollection
        System.Windows.Media.Animation.KeySpline
        System.Windows.Media.Animation.MatrixKeyFrame
        System.Windows.Media.Animation.MatrixKeyFrameCollection
        System.Windows.Media.Animation.ObjectKeyFrame
        System.Windows.Media.Animation.ObjectKeyFrameCollection
        System.Windows.Media.Animation.Point3DKeyFrame
        System.Windows.Media.Animation.Point3DKeyFrameCollection
        System.Windows.Media.Animation.PointKeyFrame
        System.Windows.Media.Animation.PointKeyFrameCollection
        System.Windows.Media.Animation.QuaternionKeyFrame
        System.Windows.Media.Animation.QuaternionKeyFrameCollection
        System.Windows.Media.Animation.RectKeyFrame
        System.Windows.Media.Animation.RectKeyFrameCollection
        System.Windows.Media.Animation.Rotation3DKeyFrame
        System.Windows.Media.Animation.Rotation3DKeyFrameCollection
        System.Windows.Media.Animation.SingleKeyFrame
        System.Windows.Media.Animation.SingleKeyFrameCollection
        System.Windows.Media.Animation.SizeKeyFrame
        System.Windows.Media.Animation.SizeKeyFrameCollection
        System.Windows.Media.Animation.StringKeyFrame
        System.Windows.Media.Animation.StringKeyFrameCollection
        System.Windows.Media.Animation.ThicknessKeyFrame
        System.Windows.Media.Animation.ThicknessKeyFrameCollection
        System.Windows.Media.Animation.Vector3DKeyFrame
        System.Windows.Media.Animation.Vector3DKeyFrameCollection
        System.Windows.Media.Animation.VectorKeyFrame
        System.Windows.Media.Animation.VectorKeyFrameCollection
        System.Windows.Media.DoubleCollection
        System.Windows.Media.ImageMetadata
        System.Windows.Media.Int32Collection
        System.Windows.Media.Media3D.Point3DCollection
        System.Windows.Media.Media3D.Vector3DCollection
        System.Windows.Media.PointCollection
        System.Windows.Media.VectorCollection

Any public static members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

When the IsFrozen property is false, a Freezable object can be accessed only from the thread on which it was created. Attempting to access it from another thread throws an InvalidOperationException. The Dispatcher.Invoke and Dispatcher.BeginInvoke methods provide support for marshalling to the correct thread.

When their IsFrozen property is true, Freezable objects are free-threaded. For more information, see Freezable Objects Overview.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.