依赖项 属性 将不会设置

Dependency Property Will Not Set

我已经以通常的方式在我的 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);
}

我做错了什么?

您在构造函数中引用了 GISMap 属性。 GISMap 属性 在构造函数中将始终为空,因为此时它没有机会被设置,这就是为什么你会得到 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;
    }
}