如何从代码隐藏(WP8)设置多个图钉?

时间:2014-03-29 07:00:14

标签: c# xaml windows-phone-8

我尝试添加多个图钉来映射。

这是XAML:

<maps:Map ZoomLevel="8" Height="500" x:Name="map1" Width="415" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,0,0">
                <toolkit1:MapExtensions.Children>
                    <toolkit1:MapItemsControl x:Name="PushpinCollection">
                        <toolkit1:MapItemsControl.ItemTemplate>
                            <DataTemplate>
                                <toolkit1:Pushpin GeoCoordinate="{Binding Coords}" Content="{Binding Name}"/>
                            </DataTemplate>
                        </toolkit1:MapItemsControl.ItemTemplate>
                    </toolkit1:MapItemsControl>
                </toolkit1:MapExtensions.Children>
            </maps:Map>

以下是代码:

protected override async void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        var xmlDataSource = new XmlDataSource();
        var sigCol = new SignalCollection(xmlDataSource);
        var allSignals = await sigCol.GetData(false, false, "UA");
        ObservableCollection<IssueDescription> signalsWithCoords = new ObservableCollection<IssueDescription>();
        foreach (var signal in allSignals)
        {
            if (signal.Coords != null)
            {
                signalsWithCoords.Add(signal);                    
            }
        }
        PushpinCollection.ItemsSource = signalsWithCoords;
    }

但最后一个字符串抛出NullReferenceException。怎么了?

1 个答案:

答案 0 :(得分:1)

我已检查PushpinCollection是否存在(不为空),以下代码应该有效:

protected override async void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    var xmlDataSource = new XmlDataSource();
    var sigCol = new SignalCollection(xmlDataSource);
    var allSignals = await sigCol.GetData(false, false, "UA");
    ObservableCollection<IssueDescription> signalsWithCoords = new ObservableCollection<IssueDescription>();
    foreach (var signal in allSignals)
    {
        if (signal.Coords != null)
        {
            signalsWithCoords.Add(signal);                    
        }
    }
    MapItemsControl PushpinCol = MapExtensions.GetChildren(map1).FirstOrDefault(x => x is MapItemsControl) as MapItemsControl;

    PushpinCol.ItemsSource = signalsWithCoords;
}

进一步挖掘事实证明这似乎是一个小错误。在源代码DependencyProperty NameProperty中注册为"ItemTemplate" - 这可能会阻止从代码中获取名称。

编辑 - 评论后

在源代码中挖掘更多信息会在Items.Count > 0时尝试更改ItemsSource时,MapExtension会抛出此异常的信息。这是一种允许您更改ItemsSource的方法:

protected override async void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    var xmlDataSource = new XmlDataSource();
    var sigCol = new SignalCollection(xmlDataSource);
    var allSignals = await sigCol.GetData(false, false, "UA");
    ObservableCollection<IssueDescription> signalsWithCoords = new ObservableCollection<IssueDescription>();
    foreach (var signal in allSignals)
    {
        if (signal.Coords != null)
        {
            signalsWithCoords.Add(signal);                    
        }
    }
    MapItemsControl PushpinCol = MapExtensions.GetChildren(map1).FirstOrDefault(x => x is MapItemsControl) as MapItemsControl;
    if (PushpinCol != null && PushpinCol.ItemsSource != null)
    {
        (PushpinCol.ItemsSource as IList).Clear();
        PushpinCol.ItemsSource = null;
    }
    PushpinCol.ItemsSource = signalsWithCoords;
}

另一方面,请考虑使您的Collection静态并将其设置为ItemsSource一次。