splash27
splash27

Reputation: 2107

How to set multiple pushpins from code-behind (WP8)?

I try to add a multiple pushpins to map.

Here's the 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>

Here's the code:

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;
    }

But the last string throws NullReferenceException. What's wrong?

Upvotes: 1

Views: 252

Answers (1)

Romasz
Romasz

Reputation: 29792

As I've checked PushpinCollection exists (is not null) and the following code should work:

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;
}

Digging a little further it turns out that this seems to be a little bug. In the source code DependencyProperty NameProperty is registered as "ItemTemplate" - which probably prevents from getting it by name from code.

EDIT - after comments

Digging a little more in the source code brigns an information that this exception is thrown by MapExtension when Items.Count > 0 and you try to change ItemsSource. Here is an approach which allows you to change 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;
}

On the other hand consider making your Collection static and set it as ItemsSource once.

Upvotes: 1

Related Questions