极品&QUOT短而明确的定义;依赖属性" [英] Need a short and clear definition for "Dependency Properties"

查看:88
本文介绍了极品&QUOT短而明确的定义;依赖属性"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚究竟依赖属性的,但是当我看到任何地方的定义,我只能找到如何使用而不是它是什么。
想象一下你被要求在面试 - 什么是依赖属性。什么是你的答案?

I'm trying to figure out what exactly Dependency Properties are, but when I look anywhere for a definition, I only find "how to use" but not "what it is". Imagine you are asked on a job interview - what is a dependency property. What would be your answer?

推荐答案

一个的DependencyProperty是(财产上的其它来源,其价值取决于(或可以依赖)等作为动画,数据绑定,样式,或可视树继承)。定期属性的值存储在对象中它属于,而你能想到的依赖项属性为存储在数据库中的某个地方。该数据库主要由映射的字典中的(对象属性)的对自己的价值观,有一个映射沿着它的性能依赖于其他属性(例如所以当你改变面板的DataContext的,它可以通知所有的面板内的儿童)。

A DependencyProperty is a property whose value depends (or can depend) on some other source (such as animation, data binding, styles, or visual tree inheritance). A regular property's value is stored in the object it belongs to, while you can think of a dependency property as being stored in a database somewhere. This database is essentially composed of a dictionary that maps (object, property) pairs to their values, along with a mapping of which properties depend on other properties (e.g. so when you change the DataContext of a Panel, it can notify all the children inside the panel).

那么,为什么他们存放在一些神奇的地方数据库属性值?有几个方面的原因:

So why do they store property values in some magic database somewhere? There are a few reasons:


  • 它减少了存储空间。添加一个属性(即使它的值是零),以一类增加的空间的4个字节(8个用于64位处理)对类的每个实例。对DependencyProperty只占用空间时,一个实例有一个值。例如,FrameworkElement的有几十个依赖属性,其中大部分是从未分配的值。如果所有这些特性进行了存储在类,每个实例将数百个字节。相反,每个实例只有大约40个字节。

  • It reduces storage space. Adding a property (even if its value is null) to a class adds 4 bytes (8 for a 64-bit process) of space to every instance of the class. A DependencyProperty only takes up space when an instance has a value. For example, a FrameworkElement has dozens of dependency properties, most of which are never assigned values. If all those properties were stored in the class, each instance would be hundreds of bytes. Instead each instance is only about 40 bytes.

它支持附加属性。像属性 Canvas.Left Grid.Row 都将存储在那些从未听过<$ C的对象$ C>画布或电网,所以你在哪里把它们?你把它们放在一个数据库的某个地方。

It enables attached properties. Properties like Canvas.Left and Grid.Row have to be stored on objects that have never heard of a Canvas or Grid, so where do you put them? You put them in a database somewhere.

它能够自动属性更改。想象一下,你将如何实现类似的风格或财产继承(设置父元素就像一个字体或数据上下文,并有它的价值传播到所有子元素的能力)。具有所有这些存储在数据库中使得它如此的代码是在同一个地方,而不是被单独实现为需要它的每个对象和属性

It enables automatic property changes. Imagine how you would implement something like styles or property inheritance (the ability to set something like a font or data context on a parent element and have its value propagate to all child elements). Having all of this stored in a database makes it so the code is all in one place instead of being implemented separately for each object and property that needs it.

这篇关于极品&QUOT短而明确的定义;依赖属性&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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