《深入浅出WPF》读书笔记.7依赖属性和附加属性

发布于:2024-08-26 ⋅ 阅读:(97) ⋅ 点赞:(0)

《深入浅出WPF》读书笔记.7依赖属性和附加属性

背景

总结一下wpf依赖属性和附加属性的底层逻辑,方便更好的理解使用。

属性

CLR属性由来

static属性和非static属性的区别

static属性:对类有意义,内存只有一个实例;

非static属性:对类实例有意义,每个类实例类都有一个实例;

CLR属性的演变

>字段

>>字段+get set方法

>>>属性

CLR属性不会增加内存损耗

依赖属性

依赖属性在使用数据时才会分配空间

DependencyObject的参数属性
        public string NameProperty
        {
            get { return (string)GetValue(NamePropertyProperty); }
            set { SetValue(NamePropertyProperty, value); }
        }

        // Using a DependencyProperty as the backing store for NameProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty NamePropertyProperty =
            DependencyProperty.Register("NameProperty", typeof(string), typeof(Student), new PropertyMetadata(0));

👆包装器>            DependencyProperty.Register("NameProperty", typeof(string), typeof(Student), new PropertyMetadata(0));

依赖属性的存取机制

DependencyObject中存在一个HashTable,用于注册DependencyObject实例,Register的源码中

的FromNameKey用于注册hashtable,key由NameProperty属性和typeof(owner)属性异或获得,

此key用于获取dependency属性。而denpency属性的实例则由GlobalIndex在依赖属性中获取

getvalue源码

依赖属性读取优先级

setvalue

附加属性

附加属性是一种特殊的依赖属性,globalindex的取值数组存储在附加对象中。


这一章是基础知识,面试可能会问,其余问题不大。原理性的东西,大家有个概念就可以。


网站公告

今日签到

点亮在社区的每一天
去签到