<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [Windbg調試WPF的依賴屬性](http://blog.csdn.net/muzizongheng/article/details/46821459)中提到了wpf的DependencyObject中DependencyProperty是如何調試查看的。 從中我們看出DO(DependencyObject)與 DP(DependencyProperty)一些內部實現。 **這篇文章我們就從源碼入手, 讓大家了解下依賴對象中依賴屬性的值的獲取和賦值。** 我們先看個DP注冊的例子: ~~~ public class MyStateControl : ButtonBase { public MyStateControl() : base() { } public Boolean State { get { return (Boolean)this.GetValue(StateProperty); } set { this.SetValue(StateProperty, value); } } public static readonly DependencyProperty StateProperty = DependencyProperty.Register( "State", typeof(Boolean), typeof(MyStateControl),new PropertyMetadata(false)); } ~~~ 上述Code中MyStateControl是DO,StateProperty是DP **1.** **當MyStateControl進行初始化, 首先會執行StateProperty, 因為它是靜態字段。從而執行DependencyProperty.Register方法。** ** ** **2.** **這個方法內部調用了DP的構造方法, Code如下:** ?// Create property ?????????? DependencyProperty dp =?new?DependencyProperty(name, propertyType, ownerType, defaultMetadata, validateValueCallback); **3.** **DP的構造方法如下:** ?private?DependencyProperty(?string?name, Type propertyType, Type ownerType, PropertyMetadata defaultMetadata, ValidateValueCallback validateValueCallback) ?????? { ?????????? _name = name; ?????????? _propertyType = propertyType; ?????????? _ownerType = ownerType; ?????????? _defaultMetadata = defaultMetadata; ?????????? _validateValueCallback = validateValueCallback; ?????????? Flags packedData; ???????????lock?(Synchronized) ?????????? { ???????????????packedData = (Flags) GetUniqueGlobalIndex(ownerType, name); ?????????????? RegisteredPropertyList.Add(?this); ?????????? } ???????????if?(propertyType.IsValueType) ?????????? { ?????????????? packedData |= Flags.IsValueType; ?????????? } ???????????if?(propertyType ==?typeof?(object)) ?????????? { ?????????????? packedData |= Flags.IsObjectType; ?????????? } ???????????if?(typeof?(Freezable).IsAssignableFrom(propertyType)) ?????????? { ?????????????? packedData |= Flags.IsFreezableType; ?????????? } ???????????if?(propertyType ==?typeof?(string)) ?????????? { ?????????????? packedData |= Flags.IsStringType; ?????????? } ???????????_packedData = packedData; ?????? } **4.** **第3點的Code中我們可以看到packedData初始值是?(Flags) GetUniqueGlobalIndex(ownerType, name);** **GetUniqueGlobalIndex其實是DP的私有靜態變量GlobalIndexCount++得到的。** 下來這段代碼可以看出: [Flags] ???????private?enum?Flags :?int ?????? { ?????????? GlobalIndexMask?????????????????????????? = 0x0000FFFF, ?????????? IsValueType?????????????????????????????? = 0x00010000, ?????????? IsFreezableType?????????????????????????? = 0x00020000, ?????????? IsStringType????????????????????????????? = 0x00040000, ?????????? IsPotentiallyInherited??????????????????? = 0x00080000, ?????????? IsDefaultValueChanged???????????????????? = 0x00100000, ?????????? IsPotentiallyUsingDefaultValueFactory???? = 0x00200000, ?????????? IsObjectType????????????????????????????? = 0x00400000, ???????????// 0xFF800000?? free bits ?????? } public?int?GlobalIndex ?????? { ???????????get?{?return?(int) (_packedData & Flags.GlobalIndexMask); } ?????? } _packedData的低4位即代表了StateProperty在整個DP數組RegisteredPropertyList中的索引。 **5.** **我們在構造里看到_packedData成員變量, 還記得我們[“windbg如何調試依賴屬性”](http://blog.csdn.net/muzizongheng/article/details/46821459)用到了它嗎?** **我們用?.formats?命令轉換去掉_packedData高位得到了DP在DO中的存儲索引。** **通過第4和第5點, 想必大家已經對DP注冊有了了解** ** ** **接下來我們再看下DO中如何獲取DP值,以及如何設置DP值。** **6.** **首先我們說下DO設置DP,Code類似:** ~~~ set { this.SetValue(StateProperty, value); } ~~~ ~~~ ~~~ ~~~ 可以看到我們通過DO的SetValue來給DP設置值。 ~~~ ~~~ ~~~ **7.** **SetValue內部實現如下:** private void SetValueCommon( ? ? ? ? ? DependencyProperty ?dp, ? ? ? ? ? object ? ? ? ? ? ? ?value, ? ? ? ? ? PropertyMetadata ? ?metadata, ? ? ? ? ? bool ? ? ? ? ? ? ? ?coerceWithDeferredReference, ? ? ? ? ? bool ? ? ? ? ? ? ? ?coerceWithCurrentValue, ? ? ? ? ? OperationType ? ? ? operationType, ? ? ? ? ? bool ? ? ? ? ? ? ? ?isInternal) ? ? ? { ? ? ? ? ?。。。。。。 ? ? ? ??? EntryIndex entryIndex = LookupEntry(dp.GlobalIndex); ? ? ? ? 。。。。。。 **大家可以看到DO根據DP的GlobalIndex在_effectiveValues數組中查找到EntryIndex, EntryIndex包含對應index和Value,如果沒有查到則在_effectiveValues中插入并返回index。** **(有興趣可以看看LookupEntry的實現)** **在數組中找到之后接下來就是往數組中賦值。代碼類似(真實比下面更復雜):** if?(entryIndex.Found) ?????????????? { ?????????????????? newEntry =?_effectiveValues[entryIndex.Index]; ?????????????? } ? ? ? ? ? ? ? **然后調用UpdateEffectiveValue發送屬性更改通知。(還有其他一些Coerce相關代碼,暫且不述)** ** // fire change notification ?????????????????? NotifyPropertyChange( ???????????????????????????new?DependencyPropertyChangedEventArgs( ?????????????????????????????????? dp, ?????????????????????????????????? metadata, ?????????????????????????????????? isAValueChange, ?????????????????????????????????? oldEntry, ?????????????????????????????????? newEntry, ?????????????????????????????????? operationType)); 通過上面我們可以了解依賴對象中的依賴屬性的賦值實現, 我們接下來再看看取值。 **8.** **DO獲取DP的值,Code類似:** ~~~ ~~~ ~~~ get { return (Boolean)this.GetValue(StateProperty); } ~~~ **9.?** **GetValue內部實現如下:** ?public?object?GetValue(DependencyProperty dp) ?????? { ? ? ? ? ?。。。 ???????????// Call Forwarded ???????????return?GetValueEntry( ???????????????????LookupEntry(dp.GlobalIndex), ?????????????????? dp, ???????????????????null, ?????????????????? RequestFlags.FullyResolved).Value; ?????? } 可以看出是先找到DP的索引,然后接下來從_effectiveValues數組中找到對應的值。代碼類似如下: entry = _effectiveValues[entryIndex.Index]; (當然其中也有一些值優先級的處理,從來獲取到正確的值) ** ** **OK, 我們再回想下Windbg中查看依賴對象的依賴屬性的值的步驟, 1.得到依賴對象的值;2.得到依賴屬性的值;3得到依賴屬性的GlobalIndex;4.根據GlobalIndex去依賴對象中的_effectiveValues找到對應index的值。** **是不是對DP和DO的一些實現更了解了呢?** ~~~ ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看