Thursday, April 19, 2007

Freezable Object

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.

Freezable having Changed event to notify any changes in the object . Freezable can improve performance because it wont take more time to spend resources on changes. A Frozen Freezable can also be shared across threads. But unfrozen cant.Although the Freezable class has many applications, most Freezable objects in Windows Presentation Foundation (WPF) are related to the graphics sub-system.

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.

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

while painting the button on the screen , the SolidColorBrush 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."

A freezable's Freeze method enables you to disable this self-updating ability. You can use this method to make the brush become "frozen," or unmodifiable.Here we are checking the object can freeze or not. Then if can means we can freeze using Freeze() method.

SolidColorBrush myBrush = new SolidColorBrush(Colors.Yellow);
if (myBrush.CanFreeze)
{
// Makes the brush unmodifiable.
myBrush.Freeze();
}

Not every Freezable object can be frozen. To avoid throwing an InvalidOperationException, check the value of the Freezable object's CanFreeze property to determine whether it can be frozen before attempting to freeze it.

Button myButton = new Button();
SolidColorBrush myBrush = new SolidColorBrush(Colors.Yellow);
myButton.Background = myBrush;
// Changes the button's background to red.
myBrush.Color = Colors.Red;

This sample first set the yellow color to Button , then will get change into the Red. after when get next page refresh. But it happening immediately after rendering the button.

Freezing a Freezable

To make a Freezable unmodifiable, you call its Freeze method. When you freeze an object that contains freezable objects, those objects are frozen as well. For example, if you freeze a PathGeometry, the figures and segments it contains would be frozen too. A Freezable can't be frozen if any of the following are true.

It has animated or data bound properties. It has properties set by a dynamic resource. (See the Resources Overview for more information about dynamic resources.) It contains Freezable sub-objects that can't be frozen.

Freezing from Markup

To freeze a Freezable object declared in markup, you 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>

To use the Freeze attribute, you must map to the presentation options namespace: http://schemas.microsoft.com/winfx/2006/xaml/presentation/options PresentationOptions is the recommended prefix for mapping this namespace:

XAML
xmlns:PresentationOptions=http://schemas.microsoft.com/winfx/2006/xaml/presentation/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:

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="PresentationOptions"

"Unfreezing" a Freezable

Once frozen, a Freezable can never be modified or unfrozen; however, you can create an unfrozen clone using the Clone or CloneCurrentValue method.

Creating Your Own Freezable Class

A class that derives from Freezable gains the following features.
  • Special states: a read-only (frozen) and a writable state.
  • Thread safety: a frozen Freezable can be shared across threads.
  • Detailed change notification: Unlike other DependencyObjects, Freezable objects provide change notifications when sub-property values change.
  • Easy cloning: the Freezable class has already implemented several methods that produce deep clones.

A Freezable is a type of DependencyObject, and therefore uses the dependency property system. Your class properties don't have to be dependency properties, but using dependency properties will reduce the amount of code you have to write, because the Freezable class was designed with dependency properties in mind. For more information about the dependency property system, see the Dependency Properties Overview which is posted by me.

Every Freezable subclass must override the CreateInstanceCore method. If your class uses dependency properties for all its data, you're finished.
If your class contains non-dependency property data members, you must also override the following methods:

  1. CloneCore
  2. CloneCurrentValueCore
  3. GetAsFrozenCoree
  4. GetCurrentValueAsFrozenCore
  5. FreezeCore

Enjoy!!!!!!!!!

2 comments:

  1. Great and informative. keep up your good work

    ReplyDelete
  2. Thanks Sir for this wonderfull and nicely informative article on Freezable Objects.... keep it up

    ReplyDelete