【问题标题】:Pushpin is null图钉为空
【发布时间】:2014-10-08 17:45:24
【问题描述】:

我有一张带有 2 个图钉的地图,当我加载页面时,我在后面的代码中设置了其中一个图钉的位置。但是,当我尝试引用该引脚时,它为空。

XAML

 <maps:Map x:Name="mapEventLocation" ZoomLevel="15" Grid.Row="1" Tap="mapEventLocation_Tap" >
        <maptoolkit:MapExtensions.Children>
            <maptoolkit:Pushpin x:Name="userLocationPin" Content="You" Visibility="Collapsed" />
            <maptoolkit:Pushpin x:Name="eventLocationPin" Content="Event" Visibility="Collapsed" />
        </maptoolkit:MapExtensions.Children>
    </maps:Map>

C#

    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        if (this.NavigationContext.QueryString.ContainsKey("lat"))
        {               
            userLocationPin.GeoCoordinate = new GeoCoordinate(double.Parse(this.NavigationContext.QueryString["lat"]), double.Parse(this.NavigationContext.QueryString["lon"]));
            userLocationPin.Visibility = System.Windows.Visibility.Visible;
            mapEventLocation.Center = new GeoCoordinate(double.Parse(this.NavigationContext.QueryString["lat"]), double.Parse(this.NavigationContext.QueryString["lon"]));
        }
    }

Lat 和 Lon 肯定在 QueryString 中。

在此之前,使用过

Pushpin pin = (Pushpin)this.FindName("userLocationPin");

这适用于模拟器,但不适用于真机。

我尝试在图层中手动制作图钉,但图钉脱落到角落

Pushpin userLocation = new Pushpin();
userLocation.GeoCoordinate = new GeoCoordinate(double.Parse(this.NavigationContext.QueryString["lat"]), double.Parse(this.NavigationContext.QueryString["lon"]));
userLocation.Content = "You";
MapLayer layer = new MapLayer();
MapOverlay overlay = new MapOverlay();
overlay.Content = userLocation;
layer.Add(overlay);
mapEventLocation.Layers.Add(layer);

任何想法将不胜感激。 谢谢

【问题讨论】:

  • 当您说您正在尝试“引用”图钉时。您以什么方式尝试引用 pin 并获得 null?

标签: c# xaml windows-phone-8 map


【解决方案1】:

我不确定问题出在哪里,但可以尝试以下几点:

  • 使用OnApplyTemplate 而不是Loaded(如果问题是加载/应用子模板的时间问题,这将有所帮助):

public override void OnApplyTemplate()
{
    Pushpin pin = (Pushpin)this.FindName("userLocationPin");
}
  • 使用附加的属性引用来获取元素(如果问题与名称范围解析有关,这将有所帮助):

public override void OnApplyTemplate()
{
    PushPin pin = MapExtensions.GetChildren(mapEventLocation)
        .FirstOrDefault() as Pushpin;
}

【讨论】:

  • OnApplyTemplate 在页面加载时不会触发。但是,使用 MapExtensions.GetChildren 确实有效。谢谢 :) 50 积分 :P
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多