揭秘依赖属性 [英] Demystifying Dependency Properties

查看:144
本文介绍了揭秘依赖属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了一下依赖属性了很多SO和其他网站。但是,真的没有找到一个很好的解释,现在还在迷茫。我同时使用SL和WPF。他们是不同的SL和WPF,在执行方面?为什么我们真的需要他们?他们是静态意味着它们的值是共享的?为什么MS的原因介绍了依赖属性?

I have read about dependency properties a lot on SO and other sites. But, really haven't found a great explanation and am still confused. I use both SL and WPF. Are they different in SL and WPF, in terms of implementation? Why do we really need them? Are they static means that their values are shared? Why was the reason MS introduced dependency properties?

赏金: 我要寻找一个更全面的,完整的答案。

Bounty: I am looking for a more thorough, complete answer.

推荐答案

答案是名字本身,虽然词依赖是如此的充满意义的软件开发,这不是特别清楚这意味着什么。

The answer is in the name itself, though the word "dependency" is so fraught with meaning in software development that it's not especially clear what that means.

一个依赖项属性是一个对象,其值的属性的依赖的一些的的对象。因此,例如:

A dependency property is a property on one object whose value depends on some other object. So, for instance:

  • 文本框可以(而且通常确实如此)的的FontFamily 属性的值取决于及其容器的FontFamily 属性。如果更改了容器上的属性,在文本框的值发生变化。

  • The value of the FontFamily property of a TextBox can (and usually does) depend on the FontFamily property of its container. If you change the property on the container, the value on the TextBox changes.

文本值 A 文本框的属性可以依赖于绑定的数据源。当绑定属性的值更改,的值文本属性的更改。

The value of the Text property of a TextBox can depend on a bound data source. When the value of the bound property changes, the value of the Text property changes.

标签的透明度属性的值可以依靠的动画故事板,在在这里你已经建立了一个用户界面元素淡入或淡出针对某些事件常见的场景。

The value of the Opacity property of a Label can depend on an animation storyboard, in the common scenario where you've set up a UI element to fade in or fade out in response to some event.

各类性质的任何UI元素的值取决于你应用了风格。

The value of all kinds of properties on any UI element can depend on the style that you've applied to them.

背后依赖的核心概念是,多数民众赞成取决于应该得到它取决于事物的属性值的东西。这就是为什么一个依赖属性实现为一个CLR属性,它的getter调用的方法。

The central concept behind dependency is that the thing that's depending should get the property value from the thing it's depending on. This is why a dependency property is implemented as a CLR property whose getter calls a method.

文本框,或者说的渲染它的东西,需要知道它的的FontFamily 是,在的FontFamily 吸气调用的GetValue 方法。该方法从容器或动画,或结合,或任何发现的值。

When the TextBox, or the thing that's rendering it, needs to know what its FontFamily is, the FontFamily getter calls the GetValue method. That method finds the value from the container, or animation, or binding, or whatever.

有很多复杂的那个方法。如果该值的继承,例如,它工作的方式,是pretty的类似于如何WPF找到风格在资源字典:它会在本地字典找到的值,如果没有进入它看起来在其父母的词典,等等所有,直到它找到一个值或达到层次结构的顶部,在这种情况下,它使用一个默认值的方式了。

There's a lot of complexity in that method. If the value's inherited, for instance, it works in a way that's pretty analogous to how WPF finds styles in a resource dictionary: it looks in a local dictionary to find the value, and if there's no entry it looks in its parent's dictionary, and so on all the way up until it finds a value or reaches the top of the hierarchy, in which case it uses a default value.

如果你看一下依赖属性的实现,这就是你会发现。一个依赖对象具有可以或可以不包含给定属性的条目字典。该的GetValue 的方法从字典中获取的值(这是与依赖属性的对象怎么会有局部值覆盖他们继承了什么),如果没有找到的价值,它使用的元信息有关的财产,以找出它应该的样子。

If you look at the implementation of dependency properties, that's what you'll find. A dependency object has a dictionary that may or may not contain an entry for a given property. The GetValue method gets values from that dictionary (which is how objects with dependency properties can have local values that override what they're inheriting from), and if it doesn't find the value, it uses the metainformation about the property to figure out where it should look.

由于该元信息是相同的类中的每一个对象(即 TextBox.Text 作品相同的每个文本框),它存储在字典类的静态属性。

Since that metainformation is the same for every object in the class (i.e., TextBox.Text works the same for every TextBox), the dictionary it's stored in is a static property of the class.

所以当你看到code是这样的:

So when you see code like this:

static Button()
{
   // Register the property
   Button.IsDefaultProperty = 
     DependencyProperty.Register("IsDefault",
     typeof(bool), typeof(Button),
     new FrameworkPropertyMetadata(false,
        new PropertyChangedCallback(OnIsDefaultChanged)));
}

这是怎么回事是定义上的所有按钮对象的 ISDEFAULT 属性的元信息被添加到字典中。当你看到这样的:

what's happening is that the metainformation that defines the IsDefault property on all Button objects is being added to that dictionary. And when you see this:

public bool IsDefault
{
  get { return (bool)GetValue(Button.IsDefaultProperty); }
  set { SetValue(Button.IsDefaultProperty, value); }
}

您现在看到的是一种基于元信息的getter方法​​中查找属性的值(从本地词典,父对象,或其他)。

what you're seeing is the getter method that looks up the property's value (from the local dictionary, the parent object, or whatever) based on that metainformation.

还记得我怎么说,首先,吸气看起来找到一个属性的值是在对象的本地词典吗?该的SetValue 的setter方法​​是进入如何被添加到字典中(如果它曾经被称为,这也只会是,如果你通过显式设置覆盖依赖财产,即说:我希望这个文本框索拉不管是什么,在窗口中的其他控件将显示文本使用)。

Remember how I said that the first place the getter looks to find a property's value is in the object's local dictionary? The SetValue method in the setter is how that entry gets added to the dictionary (if it's ever called, which it will only be if you're overriding the dependency by explicitly setting the property, i.e. saying "I want this TextBox to display text in Consolas irrespective of what the other controls in the window are using.").

这是我们从这种显然是复杂的系统中取得了巨大的显著的好处是,在对象上依赖属性只消耗内存,如果他们设置。如果我创建万文本框控件,并将它们添加到窗口,还没有一个人实际上包含引用的FontFamily 对象。这10000对象引用,我不是分配内存和垃圾收集器不检查。事实上,如果一个文本框有100个依赖属性(和它,刚想),当你创建一个文本框 ,这是100的后盾领域你是不是分配内存。依赖属性只消耗内存,如果你明确地设定。由于对UI特性的绝大多数对象永远不会显式设置,这些都是梦幻般的储蓄。

A hugely significant benefit that we get from this kind of apparently-complex system is that dependency properties on objects only consume memory if they're set. If I create 10,000 TextBox controls and add them to a Window, not one of them actually contains a reference to a FontFamily object. That's 10,000 object references that I'm not allocating memory for, and that the garbage collector isn't checking. In fact, if a TextBox has 100 dependency properties (and it does, just about), whenever you create a TextBox, that's 100 backing fields you aren't allocating memory for. Dependency properties only consume memory if you explicitly set them. Since the vast majority of properties on UI objects never get explictly set, these are fantastic savings.

这篇关于揭秘依赖属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆