Pushpin - null

I have a map with 2 Pushpins, when I load the page, I set the location of one of the buttons in the code. However, when I try to reference the output, it is zero.

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> 

FROM#

  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 and Lon are definitely in QueryString.

Used before

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

This works for an emulator, but not on a real phone.

I tried to make the pushpins manually in the layer, but the pin goes into a corner

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

Any ideas would be highly appreciated. Thanks

+5
source share
1 answer

I'm not sure what the problem is, but here are a few things to try:

  • use OnApplyTemplate , not Loaded (this will help if the problem is related to how the child templates are loaded / applied):

 public override void OnApplyTemplate() { Pushpin pin = (Pushpin)this.FindName("userLocationPin"); } 
  • use the property link to get the element (this will help if the problem is with name resolution):

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

Source: https://habr.com/ru/post/1200368/


All Articles