开发者

How do I create my own Freezable class?

I've tried MSDN but there is not an example for deriving from Freezable.

Update:

Yes in the MSDN there's an example with animations but it's too complicated. need something simpler to un开发者_Python百科derstand freezable.


Documentation

In the MSDN documentation of the Freezable class, in the Remarks section, you can find the following paragraph:

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

This overview contains a section Creating Your Own Freezable Class, which contains the theoretical background for what you want to do. To find an example, follow the link at the bottom of that section:

For an example of a custom Freezable class, see the Custom Animation Sample.


Example

Since you specifically asked for a simple example, here is one (adapted from the MSDN page of Freezable.CreateInstanceCore). Remember the following sentence from the theory page:

Every Freezable subclass must override the CreateInstanceCore method. If your class uses dependency properties for all its data, you're finished.

Let's say we make a custom class MySimpleColor, which has exactly one boolean property IsRed. To make this class Freezable, we just have to override CreateInstanceCore:

public class MySimpleColor : Freezable
{
    // Here are your properties
    public static readonly DependencyProperty IsRedProperty = 
        DependencyProperty.Register("IsRed", typeof(Boolean), typeof(MySimpleColor));

    // CLR accessor of your property
    public bool IsRed {
        get { return (bool)GetValue(IsRedProperty); }
        set { SetValue(IsRedProperty, value); }
    }

    // All that's needed to make this Freezable
    protected override Freezable CreateInstanceCore() {
        return new MySimpleColor();      
    }
}

That's it. The code inherited from Freezable ensures that the Freezable methods such as Freeze() or Clone() work exactly as intended.


What have you tried? This Link clearly states what is needed to inherit from the abstract Freezable class.

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.

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:

  • CloneCore

  • CloneCurrentValueCore

  • GetAsFrozenCore

  • GetCurrentValueAsFrozenCore

  • FreezeCore

You must also observe the following rules for accessing and writing to data members that are not dependency properties:

At the beginning of any API that reads non-dependency property data members, call the ReadPreamble method.

At the beginning of any API that writes non-dependency property data members, call the WritePreamble method. (Once you've called WritePreamble in an API, you don't need to make an additional call to ReadPreamble if you also read non-dependency property data members.)

Call the WritePostscript method before exiting methods that write to non-dependency property data members.

If your class contains non-dependency-property data members that are DependencyObject objects, you must also call the OnFreezablePropertyChanged method each time you change on of their values, even if you're setting the member to null.


check Freezable Objects Overview you can find sample at the end of the article

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜