2016-06-20 17 views
1

ich den Weg nicht finden kann eine Linie (Polylinie) mit meinem CustomMapRenderer.cs zu ziehen ..Xamarin Forms - Zeichnen Polylinie für WinPhone 8.1 Teil

var polyline = new MapPolyline(); 
polyline.StrokeColor = Windows.UI.Color.FromArgb(128, 255, 0, 0); 
polyline.StrokeThickness = 5; 
polyline.Path = new Geopath(coordinates); 
nativeMap.MapElements.Add(polyline); 

Eine Ausnahme vom Typ ‚System.Exception‘ trat in CaptainSam.WinPhone.ni.EXE, wurde jedoch nicht im Benutzercode behandelt. Zusätzliche Informationen: Schwerwiegender Fehler (Ausnahme von HRESULT: 0x8000FFFF (E_UNEXPECTED))

Die Ausnahme kommt von polyline.Path = new Geopath(coordinates);

Es ist Highlight a Route on a Map von Microsoft Tutorial, so was ist passiert?

Ich weiß, sie sagt, es ist ein Beispiel für UWP ist, aber ich habe auch versucht, und es wirft auch die gleiche Ausnahme ...

Antwort

0

Nach einer langen Zeit, fand ich, warum es nicht funktioniert hat ...

wenn Sie wie ich tun, wenn Sie ein HttpRequest etwas wie Google Richtung API tun, dann wird das Ergebnis in den OnElementChanged() nach dem ersten Durchgang kommt.

Da formsMap.RouteCoordinates ist nicht null aber leer, die Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED)) geworfen ..

Das ist die guteCustomMapRendererfür PolyLine Verwendung

using PROJECT; 
using PROJECT.UWP; 
using System.Collections.Generic; 
using Windows.Devices.Geolocation; 
using Windows.UI.Xaml.Controls.Maps; 
using Xamarin.Forms.Maps; 
using Xamarin.Forms.Maps.UWP; 
using Xamarin.Forms.Platform.UWP; 

[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))] 
namespace PROJECT.UWP 
{ 
    public class CustomMapRenderer : MapRenderer 
    { 
     MapControl nativeMap; 
     CustomMap formsMap; 

     protected override void OnElementChanged(ElementChangedEventArgs<Map> e) 
     { 
      base.OnElementChanged(e); 

      if (e.OldElement != null) 
      { 
       nativeMap = Control as MapControl; 
      } 

      if (e.NewElement != null) 
      { 
       formsMap = (CustomMap)e.NewElement; 
       nativeMap = Control as MapControl; 
       UpdatePolyLine(); 
      } 
     } 

     protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) 
     { 
      base.OnElementPropertyChanged(sender, e); 
      if (this.Element == null || this.Control == null) 
       return; 

      if (e.PropertyName == CustomMap.RouteCoordinatesProperty.PropertyName) 
      { 
       UpdatePolyLine(); 
      } 
     } 

     private void UpdatePolyLine() 
     { 
      if (formsMap != null && formsMap.RouteCoordinates.Count > 0) 
      { 
       List<BasicGeoposition> coordinates = new List<BasicGeoposition>(); 

       foreach (var position in formsMap.RouteCoordinates) 
       { 
        coordinates.Add(new BasicGeoposition() { Latitude = position.Latitude, Longitude = position.Longitude }); 
       } 

       Geopath path = new Geopath(coordinates); 
       MapPolyline polyline = new MapPolyline(); 
       polyline.StrokeColor = Windows.UI.Color.FromArgb(128, 255, 0, 0); 
       polyline.StrokeThickness = 5; 
       polyline.Path = path; 
       nativeMap.MapElements.Add(polyline); 
      } 
     } 
    } 
}