質問

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?

役に立ちましたか?

解決

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");

他のヒント

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top