Xamarin.iOS 自定义 Mapdelegate

Xamarin.iOS custom Mapdelegate

在我的 Xamarin.iOS 项目中,我有自己的委托 class 作为 MKMapViewDelegate

的子class

在 ViewController 中,我将委托设置为 MapView 的委托。

public override async void ViewDidLoad()
    {
        base.ViewDidLoad();
        mapView.delegate = new MyMapDelegate(this);
    }

现在在我的委托中 class 我为每个 Pin 添加了一个按钮。此按钮在地图上绘制叠加层。这里我必须设置mapview.delegate = null,否则App Crashes.

但是如果我设置 mapView.delegate = null 应用程序不会使用 "DequeueReusableAnnotation" 方法,当然应用程序也不会再使用 DidUpdateUserLocation。

如果我没有将 mapviews 委托设置为 null,我得到的错误:

System.InvalidOperationException: Event registration is overwriting existing delegate. Either just use events or your own delegate: testApp.iOS.MyMapDelegate MapKit.MKMapView+_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 调用 OverlayRenderer 和 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;
        }