문제

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