In my Xamarin.iOS project i have my own delegate class as subclass of MKMapViewDelegate
In the ViewController I set my delegate as delegate of the MapView.
public override async void ViewDidLoad()
{
base.ViewDidLoad();
mapView.delegate = new MyMapDelegate(this);
}
Now in my delegate class i add a Button for each Pin. This button draws an overlay to the map. Here I have to set mapview.delegate = null
, otherwise the App Crashes.
But if I set mapView.delegate = null
the App doesnt make use of the "DequeueReusableAnnotation" method and of course the Application won't make use of DidUpdateUserLocation anymore.
The Error i get if I don't set mapviews delegate to null :
System.InvalidOperationException: Event registration is overwriting existing delegate. Either just use events or your own delegate: testApp.iOS.MyMapDelegate MapKit.MKMapView+_MKMapViewDelegate
So how can i set a Delegate in Xamarin?
EDIT
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)
};
}
My Guess is maybe the Error appears cause i call methods from mapView like OverlayRenderer and Userlocation?
instead of calling:
mapview.OverlayRenderer = (mv,ol) => routeLine;
I had to override the method :
public override MKOverlayRenderer OverlayRenderer(MKMapView mapView, IMKOverlay overlay)
{
var myPolyLine= new MKPolylineRenderer(myWay.Polyline)
{
LineWidth = 2.0f,
StrokeColor = UIColor.Yellow
};
return myPolyLine;
}