不会设置依赖属性

Posted

技术标签:

【中文标题】不会设置依赖属性【英文标题】:Dependency Property Will Not Set 【发布时间】:2017-05-04 01:12:52 【问题描述】:

我已经以通常的方式在我的 GISPointSelecter 控件上设置了一个依赖属性。

public static readonly DependencyProperty GISMapProperty = DependencyProperty.Register(nameof(GISMap), typeof(GISControl), typeof(GISPointSelecter), new PropertyMetadata(null));

public GISControl GISMap

    get
    
        return (GISControl)GetValue(GISMapProperty);
    
    set
    
        SetValue(GISMapProperty, value);
    

但是当我去加载这个 xaml 时......

<Grid xmlns="http://schemas.microsoft.com/client/2007" xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" xmlns:datavis="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit" xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" xmlns:xivic="clr-namespace:Adapt.Presentation.Xivic;assembly=Adapt.Presentation.Xivic" xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" xmlns:xiviccontrols="clr-namespace:Adapt.Presentation.XivicControls;assembly=Adapt.Presentation.XivicControls" xmlns:fw="clr-namespace:System.Windows.Controls;assembly=FloatableWindow" xmlns:adaptcontrols="clr-namespace:Adapt.Presentation.Controls;assembly=Adapt.Presentation.Controls" xmlns:PivotSDK="clr-namespace:System.Windows.Controls.Pivot;assembly=System.Windows.Controls.Pivot" xmlns:query="clr-namespace:Adapt.Model.Data;assembly=Adapt.Model.Common.Silverlight" xmlns:data="clr-namespace:Adapt.Presentation.Data;assembly=Adapt.Presentation.XivicControls" xmlns:adaptdata="clr-namespace:Adapt.Model.Data;assembly=Adapt.Model.Common.Silverlight" xmlns:sys="clr-namespace:System;assembly=mscorlib">
<xiviccontrols:GISPointSelecter Name="GISMapControl" MiniMenuTrackButtons="Visible">
    <xiviccontrols:GISPointSelecter.GISMap>
        <xiviccontrols:GISControl x:Name="GISMap1" AutoMarkingEnabled="True" EsriOfflineZoomOutDisplay="300" DontRenderFullExtent="False" >

            <xiviccontrols:GISControl.LocationTypes>
                <sys:String>ShapeKey</sys:String>
            </xiviccontrols:GISControl.LocationTypes>

            <xiviccontrols:GISControl.LocationReferences>
                <sys:String>Asset.GISKey</sys:String>
            </xiviccontrols:GISControl.LocationReferences>

            <xiviccontrols:GISControl.OverlayReferences>
                <sys:String>, TaskDetail_, , Open Task, New Task</sys:String>
            </xiviccontrols:GISControl.OverlayReferences>

            <xiviccontrols:GISControl.MarkerConditionEquations>
                <sys:String>OverallResultTag = Red</sys:String>
                <sys:String>OverallResultTag = Yellow</sys:String>
                <sys:String>OverallResultTag = Orange</sys:String>
                <sys:String>OverallResultTag = Blue</sys:String>
                <sys:String>OverallResultTag = No</sys:String>
                <sys:String>OverallResultTag = </sys:String>
            </xiviccontrols:GISControl.MarkerConditionEquations>

            <xiviccontrols:GISControl.MarkerConditionColours>
                <sys:String>Red</sys:String>
                <sys:String>Yellow</sys:String>
                <sys:String>Orange</sys:String>
                <sys:String>Blue</sys:String>
                <sys:String>Green</sys:String>
                <sys:String>White</sys:String>
            </xiviccontrols:GISControl.MarkerConditionColours>

            <xiviccontrols:GISControl.AutoMarkLayers>
                <xiviccontrols:AutoMarkLayer AssetForeignKey="Asset" LayerKey="f38d3a14-9fec-495c-b524-c96bec5443f7" MinExtentLongitude="4000" MarkerDataType="Adapt.Model.TaskManagement.TaskInfo">
                    <xiviccontrols:AutoMarkLayer.MarkerDataQuery>

                        <adaptdata:DataQuery FromTableAlias="Tasks">
                            <adaptdata:DataQuery.WhereClauses>
                                <adaptdata:WhereClauseJoiner>OpenBracket</adaptdata:WhereClauseJoiner>
                                <adaptdata:WhereClause LeftExpression="Tasks.Activity" WhereClauseType="Equals" RightExpression="1" />
                                <adaptdata:WhereClauseJoiner>Or</adaptdata:WhereClauseJoiner>
                                <adaptdata:WhereClause LeftExpression="Tasks.Activity" WhereClauseType="Equals" RightExpression="5" />
                                <adaptdata:WhereClauseJoiner>Or</adaptdata:WhereClauseJoiner>
                                <adaptdata:WhereClause LeftExpression="Tasks.Activity" WhereClauseType="Equals" RightExpression="6" />
                                <adaptdata:WhereClauseJoiner>Or</adaptdata:WhereClauseJoiner>
                                <adaptdata:WhereClause LeftExpression="Tasks.Activity" WhereClauseType="Equals" RightExpression="7" />
                                <adaptdata:WhereClauseJoiner>CloseBracket</adaptdata:WhereClauseJoiner>
                                <adaptdata:WhereClauseJoiner>And</adaptdata:WhereClauseJoiner>
                                <adaptdata:WhereClause LeftExpression="Tasks.StatusKey" WhereClauseType="Equals" RightExpression="2" />
                            </adaptdata:DataQuery.WhereClauses>
                        </adaptdata:DataQuery>

                    </xiviccontrols:AutoMarkLayer.MarkerDataQuery>
                </xiviccontrols:AutoMarkLayer>
            </xiviccontrols:GISControl.AutoMarkLayers>

        </xiviccontrols:GISControl>
    </xiviccontrols:GISPointSelecter.GISMap>
</xiviccontrols:GISPointSelecter>

...该属性未设置,默认为 null,然后我在 GISPointSelecter 构造函数中得到 NullReferenceException。

public GISPointSelecter()

    InitializeComponent();

    GISMap.LeftIndent = 145;
    LayoutRoot.Children.Insert(0, GISMap);
    GISMap.Click += new GISControl.GISControlClickedDelegate(Map_Click);
    GISMap.GISRequestedCloseSplash += new EventHandler<EventArgs>(GISMap_GISRequestedCloseSplash);

    ViewAssetsMiniMenu.XamlProviderLoaded += new EventHandler<EventArgs>(ViewAssetsMiniMenu_XamlProviderLoaded);

我做错了什么?

【问题讨论】:

【参考方案1】:

您在构造函数中引用了 GISMap 属性。 GISMap 属性在构造函数中将始终为 null,因为此时它没有机会被设置,这就是您得到 NullReferenceException 的原因。

您应该注册一个PropertyChangedCallback 以获得有关属性值更改的通知:

public static readonly DependencyProperty GISMapProperty = DependencyProperty.Register(
    nameof(GISMap), typeof(GISControl), typeof(GISPointSelecter),
    new PropertyMetadata(GISMapPropertyChanged));

private static void GISMapPropertyChanged(
    DependencyObject o, DependencyPropertyChangedEventArgs e)

    ((GISPointSelecter)o).GISMapPropertyChanged(
        (GISControl)e.OldValue, (GISControl)e.NewValue);


private void GISMapPropertyChanged(GISControl oldGisMap, GISControl newGisMap)

    if (oldGisMap != null)
    
        LayoutRoot.Children.Remove(oldGisMap);
        oldGisMap.Click -= Map_Click;
        oldGisMap.GISRequestedCloseSplash -= GISMap_GISRequestedCloseSplash;
    

    if (newGisMap != null)
    
        LayoutRoot.Children.Insert(0, newGisMap);
        newGisMap.Click += Map_Click;
        newGisMap.GISRequestedCloseSplash += GISMap_GISRequestedCloseSplash;
    

【讨论】:

以上是关于不会设置依赖属性的主要内容,如果未能解决你的问题,请参考以下文章

1. 依赖项属性 简单理解

EmberJS Mirage 依赖属性不会在补丁上更新

WPF UserControl:使多个链接的依赖项属性保持同步,而不会导致递归循环 堆栈溢出

依赖注入到 get;设置; 属性

Vue 计算属性不会在更新其反应性依赖项时更新

背水一战 Windows 10 (67) - 控件(控件基类): DependencyObject - CoreDispatcher, 依赖属性的设置与获取, 依赖属性的变化回调