Windows phone 8.1 Xamarin表单-为WinPhone 8.1零件绘制多段线
我找不到使用CustomMapRenderer.cs绘制直线(多段线)的方法Windows phone 8.1 Xamarin表单-为WinPhone 8.1零件绘制多段线,windows-phone-8.1,maps,xamarin.forms,Windows Phone 8.1,Maps,Xamarin.forms,我找不到使用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); C
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);
CaptainSam.WinPhone.ni.EXE中发生“System.exception”类型的异常,但未在用户代码中处理。
其他信息:灾难性故障(HRESULT异常:0x8000FFFF(E_意外))
异常来自polyline.Path=new Geopath(坐标)代码>
这是微软教程的一部分,发生了什么
我知道他们说这是UWP的一个例子,但我也试过了,它也抛出了同样的异常…过了很长时间,我发现了为什么它不起作用
如果你像我一样做,如果你对Google Direction API这样的东西做一个HttpRequest
,那么结果将出现在OnElementChanged()
中的第一段之后
由于formsMap.RouteCoordinates
不是null
而是空的,因此会引发灾难性故障(来自HRESULT的异常:0x8000FFFF(E_意外))
这是用于多段线的好的自定义地图渲染器
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);
}
}
}
}
使用项目;
使用PROJECT.UWP;
使用System.Collections.Generic;
使用Windows.Devices.Geolocation;
使用Windows.UI.Xaml.Controls.Maps;
使用Xamarin.Forms.Maps;
使用Xamarin.Forms.Maps.UWP;
使用Xamarin.Forms.Platform.UWP;
[程序集:ExportRenderer(typeof(CustomMap)、typeof(CustomMapRenderer))]
名称空间项目.UWP
{
公共类CustomMapRenderer:MapRenderer
{
地图控制本地地图;
自定义地图格式;
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(e.OldElement!=null)
{
nativeMap=控件作为MapControl;
}
if(例如NewElement!=null)
{
formsMap=(CustomMap)e.NewElement;
nativeMap=控件作为MapControl;
UpdatePolyLine();
}
}
受保护的覆盖无效OneElementPropertyChanged(对象发送方,System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(发送方,e);
if(this.Element==null | | this.Control==null)
返回;
如果(e.PropertyName==CustomMap.RouteCoordinationProperty.PropertyName)
{
UpdatePolyLine();
}
}
私有void UpdatePolyLine()
{
如果(formsMap!=null&&formsMap.RouteCoordinates.Count>0)
{
列表坐标=新列表();
foreach(表格中的var位置映射路线坐标)
{
添加(新的BasicGeoposition(){纬度=位置.纬度,经度=位置.经度});
}
地理路径路径=新的地理路径(坐标);
MapPolyline polyline=新的MapPolyline();
polyline.StrokeColor=Windows.UI.Color.FromArgb(128255,0,0);
折线.StrokeThickness=5;
多段线。路径=路径;
nativeMap.MapElements.Add(多段线);
}
}
}
}
过了很长一段时间,我发现了为什么它不起作用
如果你像我一样做,如果你对Google Direction API这样的东西做一个HttpRequest
,那么结果将出现在OnElementChanged()
中的第一段之后
由于formsMap.RouteCoordinates
不是null
而是空的,因此会引发灾难性故障(来自HRESULT的异常:0x8000FFFF(E_意外))
这是用于多段线的好的自定义地图渲染器
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);
}
}
}
}
使用项目;
使用PROJECT.UWP;
使用System.Collections.Generic;
使用Windows.Devices.Geolocation;
使用Windows.UI.Xaml.Controls.Maps;
使用Xamarin.Forms.Maps;
使用Xamarin.Forms.Maps.UWP;
使用Xamarin.Forms.Platform.UWP;
[程序集:ExportRenderer(typeof(CustomMap)、typeof(CustomMapRenderer))]
名称空间项目.UWP
{
公共类CustomMapRenderer:MapRenderer
{
地图控制本地地图;
自定义地图格式;
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(e.OldElement!=null)
{
nativeMap=控件作为MapControl;
}
if(例如NewElement!=null)
{
formsMap=(CustomMap)e.NewElement;
nativeMap=控件作为MapControl;
UpdatePolyLine();
}
}
受保护的覆盖无效OneElementPropertyChanged(对象发送方,System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(发送方,e);
if(this.Element==null | | this.Control==null)
返回;
如果(e.PropertyName==CustomMap.RouteCoordinationProperty.PropertyName)
{
UpdatePolyLine();
}
}
私有void UpdatePolyLine()
{
如果(formsMap!=null&&formsMap.RouteCoordinates.Count>0)
{
列表坐标=新列表();
foreach(表格中的var位置映射路线坐标)
{
添加(新的BasicGeoposition(){纬度=位置.纬度,经度=位置.经度});
}
地理路径路径=新的地理路径(坐标);
MapPolyline polyline=新的MapPolyline();
polyline.StrokeColor=Windows.UI.Color.FromArgb(128255,0,0);
折线.StrokeThickness=5;
多段线。路径=路径;
nativeMap.MapElements.Add(多段线);
}
}
}
}