Freezable Objects in WPF

Freezable Objects in WPF

I have lot of experience working with the GDI+, which is the API responsible for drawing and objects in .NET 1.0 and 2.0. Performance of rendering objects was one of the biggest issues in GDI+ specially if you are drawing complex images and updating them rapidly.

If you read Introduction to XAML chapter, you must know how events are implemented in .NET 3.5. Each object (including shapes) fires many events from time to time and every time an event is fired, objects have to render again on the UI. The rendering process includes getting the details of the object and draw pixels on the screen every time any changes is made to an object. For example, in GDI+, we usually draws objects on the paint event handler and every time a form is resized, moved, or any other action is taken on the form, the paint event is fired and rendering process draws the objects again.  

However, .NET 3.5 introduces a new type of objects called freezable objects. These objects are usually related to graphics sub-system.

A freezable object has two states – frozen and unfrozen. When an object is frozen, it cannot be modified and it cannot even fires events but the unfrozen state, it works same as a normal object. When object is in the frozen state, it avoids all of the re-rendering process and hence the performance of the entire rendering process of a window and its controls is much faster. Some freezable objects are – Brush, Pen, Geometry, Transform, and AnimationTimeline.

You can create your own freezable objects by inheriting an object from the Freezable class. Once a class inherited from the Freezable, it automatically gets frozen and unfrozen states and can be shared among multiple threads. A freezable object also gets cloning functionality.

Freezable Class Properties and Methods

The Freezable class has two properties.

The CanFreeze property represents if an objects can be frozen or not, means an object is inherited from a Freezable class. The IsFrozen property represents of an object is frozen or not.

The Freeze method makes an object frozen by setting its IsFrozen property to true. That means an object will stop receiving event notifications once this method is called. The FreezeCore method calls the CanFreeze property to check if an object can be frozen. After that it calls the Freeze method to freeze an object if CanFrozen is true.

The GetAsFrozen method creates a copy of the Freezable object. To create copy of an object if it is not in frozen state, use the Clone method. The GetCurrentValueAsFrozen method creates a frozen copy of an object using current property values.

Here is an example that uses CanFreeze and IsFrozen properties.

 

Button AButton = new Button();

SolidColorBrush GreenBrush = new SolidColorBrush(Colors.Green);

 

if (GreenBrush.CanFreeze)

{

   GreenBrush.Freeze();

}           

 

AButton.Background = GreenBrush;

 

if (GreenBrush.IsFrozen)

{

   // Do something

}

else

{

    // Brush can be modified

}

 

Ebook Download
View all
Learn
View all