Xamarin Forms - 为 WinPhone 8.1 部分绘制折线

Xamarin Forms - Draw polyline for WinPhone 8.1 part

我找不到用我的 CustomMapRenderer.cs..

画线(折线)的方法
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);

'System.Exception' 类型的异常发生在 CaptainSam.WinPhone.ni.EXE 中,但未在用户代码中处理。 其他信息:灾难性故障(HRESULT 异常:0x8000FFFF (E_UNEXPECTED))

异常来自polyline.Path = new Geopath(coordinates);

是微软教程的Highlight a Route on a Map,怎么回事?

我知道他们说这是 UWP 的一个例子,但我也试过了,它也抛出了同样的异常...

弄了半天,发现为什么不行了...

如果你像我一样,如果你对Google方向APIHttpRequest,那么结果将在 OnElementChanged().

的第一段之后出现

因为 formsMap.RouteCoordinates 不是 null 而是 ,所以抛出 Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))..

这是好的 CustomMapRenderer for PolyLine use

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