Xamarin.iOS自定义Mapdelegate
在我的Xamarin.iOS项目中,我有自己的委托类作为MKMapViewDelegate的子类 在ViewController中,我将我的委托设置为MapView的委托Xamarin.iOS自定义Mapdelegate,xamarin.ios,delegates,mkmapview,Xamarin.ios,Delegates,Mkmapview,在我的Xamarin.iOS项目中,我有自己的委托类作为MKMapViewDelegate的子类 在ViewController中,我将我的委托设置为MapView的委托 public override async void ViewDidLoad() { base.ViewDidLoad(); mapView.delegate = new MyMapDelegate(this); } 现在在我的delegate类中,我为每个Pin添加了一个按钮
public override async void ViewDidLoad()
{
base.ViewDidLoad();
mapView.delegate = new MyMapDelegate(this);
}
现在在我的delegate类中,我为每个Pin添加了一个按钮。此按钮在地图上绘制覆盖图。这里我必须设置mapview.delegate=null
,否则应用程序会崩溃
但是如果我设置mapView.delegate=null
应用程序就不会使用“DequeueReusableAnnotation”方法,当然应用程序也不会再使用DidUpdateUserLocation
如果未将mapviews委托设置为null,则会出现错误:
System.InvalidOperationException:事件注册正在覆盖现有委托。只使用事件或您自己的委托:testApp.iOS.MyMapDelegate MapKit.MKMapView+\u MKMapViewDelegate
那么如何在Xamarin中设置委托
编辑
class MyMapDelegate: MKMapViewDelegate
{
... some vars
public MyMapDelegate(MapController mapController)
{
this.mapController = mapController;
}
public override MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
{
//everything works fine here for AnnotationView
myBtn += (sender,e) => {
//mapView.Delegate = null;
userLocation = mapView.UserLocation.Coordinate
...
mapView.OverlayRenderer = (mv,ol) => myLine;
mapView.AddOverlay(myWay.polyLine,MKOverlayLevel.AboveRoads)
};
}
我猜可能是因为我从mapView调用了OverlyRenderer和Userlocation等方法而出现了错误 而不是呼叫:
mapview.OverlayRenderer = (mv,ol) => routeLine;
我必须重写该方法:
public override MKOverlayRenderer OverlayRenderer(MKMapView mapView, IMKOverlay overlay)
{
var myPolyLine= new MKPolylineRenderer(myWay.Polyline)
{
LineWidth = 2.0f,
StrokeColor = UIColor.Yellow
};
return myPolyLine;
}
您可以添加
MyMapDelegate
类的代码吗?特别是在地图上创建和添加注释的地方。我编辑了我的问题@apineda