In the Microsoft .net world WPF based applications are still being developed in many organizations. Microsoft had introduced WPF in v 3.0 of .Net framework. Lots of new features and capabilities are being added in it as of v5.0.One such option is Freezable classes. Surprisingly it was introduced in v3.0 of .net framework but still little is known.
When the WPF application size becomes very big the challenges related to runtime performance of the application start surfing up.
There can be multiple ways to improve the application performance. One such option is using Freezable class.
Definition of Freezable
As per Microsoft,
A Freezable object 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. So the objects dervied from Freezable based class will have only 2 states either Frozen or Unfrozen.
Freezing a Freezable can improve its 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.
Although the Freezable class has many applications, most Freezable objects in Windows Presentation Foundation (WPF) are related to the graphics sub-system.
If required you can derive classes from Freezable class (namespace: System.Windows, Assembly: WindowsBase.dll). Classes that derive from this class provide detailed change notification, can be made immutable, and can clone themselves.
Some common examples of classes derived from Freezable class are: Brush Pen Geometry Transform AnimationTimelineetc.
How to use
When derived from Freezable based class the objects are normally in Unfrozen (read/write) state.
The objects can be set to Frozen state either from XAML or from code behind. Code behind logic is much earlier to use.
- SolidColorBrush brush = newSolidColorBrush(Colors.Green);
- if (brush.CanFreeze)
- {
-
- brush.Freeze();
- }
Now you should not try change any of the property brush Object. Doing so will throw the InvalidOperationException exception.
Another way to set it is from XAML
- <SolidColorBrushx: Freeze="True"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"/>
More examples:
- Button button = new Button();
- SolidColorBrush brush = new SolidColorBrush(Colors.Blue);
- if (brush.CanFreeze)
- {
-
- brush.Freeze();
- }
- button.Background = brush;
- if (brush.IsFrozen)
- {
-
- SolidColorBrushbrushClone = brush.Clone();
- brushClone.Color = Colors.Red;
- button.Background = brushClone;
- }
- else
- {
- brush.Color = Colors.Yellow;
- }
Another example:
- BitmapImage image = new BitmapImage(new Uri("ABC.jpg”,UriKind.RelativeOrAbsolute));
- var image2 = new Image();
- Image2.Source = image;
- grid.Children.Add(image2);
- this code looks absolute fine. However this code is potential source of memory leak due to statement grid.Children.Add(image);
- Rewrite of this code would be:
-
- BitmapImage image = new BitmapImage(new Uri("ABC.jpg”,UriKind.RelativeOrAbsolute));
- var image2 = new Image();
- Image2.Source = image;
- If (image.CanFreeze)
- {
- image.Freeze();
- }
- grid.Children.Add(image2);
If you are willing to create your own class inherited from Freezable class, you need to take care of few things.