silverlight DependencyObject和DependencyProperty詳解

來源:互聯網
上載者:User

之前我們多次講到綁定,它會有一個通知作用,那這個通知作用是怎麼出來的呢,為什麼我們以前的CLR的屬性就沒有這種通知功能呢?

那是因為現在我們SL中多了這樣兩個概念--- DependencyObject和DependencyProperty。

DependencyProperty實際上就是在DependencyObject中去使用的。

 

下面分別來介紹這兩個東東。

一、DependencyObject

1、它是SL內建定義好的,並擁有DependencyProperty的類。

2、它是一個準備要去綁定的一個資料對象  

3、我們所有的介面元素是由 DependencyObject 派生的。

 來一個UML圖

 

一、DependencyProperty

•Parameters for Registration–Name of Property–Type of Property–Type of Owner–PropertyMetadata•Types of Registration–Simple Property ‘DependencyProperty.Register’     --註冊相依性屬性–Attach Property ‘DependencyProperty.RegisterAttached’   --註冊附加屬性–Sample: Canvas.Top, Grid.Column•Principle of Dependency Property–DependencyObject.Dictionary<propertyName,  propertyValue>•Why Use Dependency Property–Binding, Inherit, Multiple Value Providers, Attachment

 

 

 

為什麼DependencyProperty能提供綁定、繼承。。。。這樣的功能呢。

下面來說說他的原理:

實際上它是在DependencyObject裡面的一個Dictionary, ,這個Dictionary包含了一系列的propertyName , 和 propertyValue 這種名值對,也就是

說我們像剛才的用一個名字去取得它對應的值。用這樣的方式,將屬性和類之前解耦了。所以我們在對這些屬性做一些特徵的時候,可以單獨得對這些屬性操作。

下面用代碼來實現DependencyObject和DependencyProperty的原理(注意:只是模仿,SL中的DependencyProperty要更複雜,但原理差不多這樣)

自已定義一個DependencyObject

    public class MyDependencyObject    {        #region Dependency Property Principle Code        private Dictionary<string, object> properties = new Dictionary<string, object>();        protected object GetValue(MyDependencyProperty property)        {            if (property.OwnerType == this.GetType())            {                return this.properties[property.Name];            }            else            {                // Sample for inherit principle.                MyDependencyObject parent = this.GetParentElement(property.OwnerType);                return parent.GetValue(property);            }        }        protected void SetValue(MyDependencyProperty property, object value)        {            this.properties[property.Name] = value;        }        #endregion        #region Private Methods        private MyDependencyObject GetParentElement(Type type)        {            // TODO: Get parent element here.            return null;        }        #endregion    }

 

自己定義一個DependencyProperty

    public class MyDependencyProperty    {        #region Constructor        private MyDependencyProperty(string name, Type valueType, Type ownerType, object defaultValue)        {            this.Name = name;            this.ValueType = valueType;            this.OwnerType = ownerType;            this.DefaultValue = defaultValue;        }        public static MyDependencyProperty Register(string name, Type valueType, Type ownerType, object defaultValue)        {            MyDependencyProperty instance = new MyDependencyProperty(name, valueType, ownerType, defaultValue);            return instance;        }        #endregion        #region CLR Properties        public string Name        {            get;            private set;        }        public Type ValueType        {            get;            private set;        }        public Type OwnerType        {            get;            private set;        }        public object DefaultValue        {            get;            private set;        }        #endregion    }

 

樣本:

    public class MyParentElement : MyDependencyObject    {        public string Text        {            get { return (string)GetValue(TextProperty); }            set { SetValue(TextProperty, value); }        }        public static readonly MyDependencyProperty TextProperty =            MyDependencyProperty.Register("Text", typeof(string), typeof(MyParentElement), "I'm parent.");    }    public class MyChildElement : MyDependencyObject    {        public string Text        {            get { return (string)GetValue(TextProperty); }            set { SetValue(TextProperty, value); }        }        public static readonly MyDependencyProperty TextProperty =            MyDependencyProperty.Register("Text", typeof(string), typeof(MyParentElement), "I'm child.");    }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.