如何创建我自己的 Freezable 类?

发布于 2024-12-02 08:25:38 字数 119 浏览 1 评论 0原文

我尝试过 MSDN,但没有从 Freezable 派生的示例。

更新:

是的,MSDN 中有一个动画示例,但它太复杂了。 需要一些更简单的东西来理解可冻结。

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 understand freezable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心如荒岛 2024-12-09 08:25:38

文档

Freezable 类的 MSDN 文档中,在备注部分,您可以找到以下段落:

有关使用和创建您自己的 Freezable 对象的信息,请参阅Freezable 对象概述< /a>.


本概述包含创建您自己的 Freezable 类部分,其中包含您想要执行的操作的理论背景。要查找示例,请点击该部分底部的链接:

有关自定义 Freezable 类的示例,请参阅自定义动画示例


示例

由于您特别要求一个简单的示例,因此这里有一个(改编自 Freezable.CreateInstanceCore 的 MSDN 页面)。请记住理论页面中的以下句子:

每个 Freezable 子类都必须重写 CreateInstanceCore 方法。如果您的类对其所有数据使用依赖属性,那么您就完成了。

假设我们创建了一个自定义类 MySimpleColor,它只有一个布尔属性 IsRed。要使此类可 Freezable,我们只需重写 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();      
    }
}

就是这样。从 Freezable 继承的代码确保 Freezable 方法(例如 Freeze()Clone())的工作方式与故意的。

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.

抱着落日 2024-12-09 08:25:38

你尝试了什么?
此链接清楚地说明了从 抽象 Freezable 类

Freezable 是 DependencyObject 的一种类型,因此使用依赖属性系统。您的类属性不必是依赖属性,但使用依赖属性将减少您必须编写的代码量,因为 Freezable 类在设计时考虑了依赖属性。有关依赖属性系统的更多信息,请参阅依赖属性概述。

每个 Freezable 子类都必须重写 CreateInstanceCore 方法。如果您的类对其所有数据使用依赖属性,那么您就完成了。

如果您的类包含非依赖属性数据成员,您还必须重写以下方法:

  • CloneCore

  • 克隆CurrentValueCore

  • GetAsFrozenCore

  • 获取CurrentValueAsFrozenCore

  • FreezeCore

您还必须遵守以下访问和写入非依赖属性的数据成员的规则:

在读取非依赖属性数据成员的任何 API 的开头,调用 ReadPreamble 方法。

在任何写入非依赖属性数据成员的 API 的开头,调用 WritePreamble 方法。 (在 API 中调用 WritePreamble 后,如果您还读取非依赖属性数据成员,则无需额外调用 ReadPreamble。)

在退出写入非依赖属性数据成员的方法之前调用 WritePostscript 方法。

如果您的类包含属于 DependencyObject 对象的非依赖属性数据成员,则每次更改其值时也必须调用 OnFreezablePropertyChanged 方法,即使您将该成员设置为 null。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文