Swift Sharp
Swift Sharp

Reputation: 2623

Assign geo coordinates to a pushpin

Guys i really need your help, i have a map

xmlns:maps="clr-namespace:Microsoft.Phone.Maps.Controls;assembly=Microsoft.Phone.Maps"
xmlns:maptk="clr-namespace:Microsoft.Phone.Maps.Toolkitassembly=Microsoft.Phone.Controls.Toolkit"

         <maps:Map Grid.Row="0"  Tap="Onm_MapControlTap" Name="m_MapControl
            <maptk:MapExtensions.Children>
                <maptk:Pushpin x:Name="m_PushPin" Tap="OnPushPinTap">
                  <maptk:Pushpin.Content>
                    <StackPanel>                        
                            <TextBlock Text=""/>
                            <Image Source=""/>
                    </StackPanel>
                </maptk:Pushpin.Content>
            </maptk:Pushpin>
           </maptk:MapExtensions.Children>
        </maps:Map>

when i have coordinates from the geowatcher, or when i tap on the map, i wanna add those coordinates to my pushpin.

m_PushPin.GeoCoordinate = m_watcher.Position.Location;

An exception of type 'System.NullReferenceException' occurred for m_PushPin, i cannot understand why this PushPin is null. Can you tell me what i'm doing wrong, and how can i fix this?

Upvotes: 2

Views: 588

Answers (2)

Aleksei Kosozhikhin
Aleksei Kosozhikhin

Reputation: 132

As an alternative you could use binding to bind some GeoCoordinate to your pushpin.

Upvotes: 1

JustinAngel
JustinAngel

Reputation: 16102

This is a known issue with the XAML Parser. Since your PushPin is nested in an Attached Dependency Property (MapExtensions.Children) it won't be part of the same logical tree as the UserControl and as such won't get picked up by FindName in that scope.

Instead, you should manually traverse the visual tree and get the control you need. Luckily, the map control ships with helper methods just for this occasion.

    var pushpin = MapExtensions.GetChildren(myMap).OfType<Pushpin>().First(p => p.Name == "m_PushPin");

Upvotes: 3

Related Questions