Xamarin.forms Forms-MKMapView iOS渲染
我尝试使用Xamarin.Forms.Maps,但它缺少许多功能,因此我决定使用MKMapView制作iOS ViewRenderer并添加自定义pin图像等,但我不确定如何制作ViewRenderer以及MKMapView如何工作。有没有人能告诉我它是如何工作的,并给我一个简单的例子,只是显示地图Xamarin.forms Forms-MKMapView iOS渲染,xamarin.forms,Xamarin.forms,我尝试使用Xamarin.Forms.Maps,但它缺少许多功能,因此我决定使用MKMapView制作iOS ViewRenderer并添加自定义pin图像等,但我不确定如何制作ViewRenderer以及MKMapView如何工作。有没有人能告诉我它是如何工作的,并给我一个简单的例子,只是显示地图 我只想为ios制作一个自定义渲染器,该渲染器将显示MKMapView,其余的我可能会想出来,但我甚至想不出如何通过viewrenderer进行显示。关于如何构建自定义viewrenderer的简单示
我只想为ios制作一个自定义渲染器,该渲染器将显示MKMapView,其余的我可能会想出来,但我甚至想不出如何通过viewrenderer进行显示。关于如何构建自定义viewrenderer的简单示例 在PCL项目中,创建从视图中删除的内容:
public class CustomMap: View
{
public static readonly BindableProperty PinsItemsSourceProperty = BindableProperty.Create ("PinsItemsSource ", typeof(IEnumerable), typeof(CustomMap), null, BindingMode.OneWay, null, null, null, null);
public IEnumerable PinsItemsSource {
get {
return (IEnumerable)base.GetValue (CustomMap.PinsItemsSourceProperty );
}
set {
base.SetValue (CustomMap.PinsItemsSourceProperty , value);
}
}
}
然后在IOS上为该视图创建自定义渲染器,如下所示:
[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer ))]
namespace Xamarin.Forms.Labs.iOS.Controls
{
public class CustomMapRenderer : ViewRenderer<CustomMap,MKMapView >
{
protected override void OnElementChanged (ElementChangedEventArgs<CustomMap> e)
{
base.OnElementChanged (e);
var mapView = new MKMapView (this.Bounds);
mapView.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;
foreach(item in e.NewElement.PinsItemsSource )
{
//add the points
var annotation = new BasicMapAnnotation (new CLLocationCoordinate2D(x,y), "something", "something");
mapView.AddAnnotation(annotation);
}
base.SetNativeControl(mapView);
}
}
[程序集:ExportRenderer(typeof(CustomMap)、typeof(CustomMapRenderer))]
命名空间Xamarin.Forms.Labs.iOS.Controls
{
公共类CustomMapRenderer:ViewRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
碱基。一个元素改变(e);
var mapView=new MKMapView(this.Bounds);
mapView.AutoresizingMask=UIViewAutoresizing.FlexibleDimensions;
foreach(e.NewElement.pinsitemSource中的项目)
{
//加分
var注释=新的BasicPannotation(新的CLLocationCoordinate2D(x,y),“某物”,“某物”);
mapView.AddAnnotation(注释);
}
base.SetNativeControl(地图视图);
}
}
ps:我是在头脑中快速编写这段代码的,我没有进行测试,但它应该可以帮助您继续工作