Custom Renderer iOS not being called at all (Xamarin.Forms.Maps Plugin)

Leonard Harris 6 Reputation points
2021-03-01T00:36:08.323+00:00

Ive got a strange issue here the custom render for iOS is not working, the OnElementChanged is not even being called at all.

I'm using the Xamarin.Forms.Maps plugin and using a custom renderer for iOS and android for some platform specific functionality required, android works fine on the renderer but iOS its not even being call it at all??

Basically looks like this in the iOS project:

[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]

namespace SampleApp.iOS.Handlers
{
public class CustomMapRenderer : MapRenderer
{
private UIView _customPinView;
private List<CustomStorePin> _customStorePins;

    protected override void OnElementChanged(ElementChangedEventArgs<View> e)
    {
        base.OnElementChanged(e);

        if (e.OldElement != null)
        {
            if (Control is MKMapView nativeMap)
            {
                nativeMap.RemoveAnnotations(nativeMap.Annotations);
                nativeMap.GetViewForAnnotation = null;
                nativeMap.CalloutAccessoryControlTapped -= OnCalloutAccessoryControlTapped;
                nativeMap.DidSelectAnnotationView -= OnDidSelectAnnotationView;
                nativeMap.DidDeselectAnnotationView -= OnDidDeselectAnnotationView;
            }
        }

        if (e.NewElement != null)
        {
            CustomMap formsMap = (CustomMap)e.NewElement;
            MKMapView nativeMap = Control as MKMapView;
            _customStorePins = formsMap.CustomPins.ToList();

            formsMap.MapPinSelectedEventHandler += FormsMap_MapPinSelectedEventHandler;

            nativeMap.GetViewForAnnotation = GetViewForAnnotation;
            nativeMap.CalloutAccessoryControlTapped += OnCalloutAccessoryControlTapped;
            nativeMap.DidSelectAnnotationView += OnDidSelectAnnotationView;
            nativeMap.DidDeselectAnnotationView += OnDidDeselectAnnotationView;
        }
    }


    }

    private void OnTap(UITapGestureRecognizer recognizer)
    {
        CGPoint cgPoint = recognizer.LocationInView(Control);
        CoreLocation.CLLocationCoordinate2D location = ((MKMapView)Control).ConvertPoint(cgPoint, Control);

        PerformSelector(new ObjCRuntime.Selector("ShowAnnotation"), null, 0.6f);
    }

    [Export("ShowAnnotation")]
    private void ShowAnnotation()
    {
        ((MKMapView)Control).SelectAnnotation(((MKMapView)Control).Annotations[0], true);
    }
}

}

My shared project has CustomMap class:

public class CustomMap : Map
{
}

I've put break points in the CustomMap renderer class in the iOS project and not a single break point gets hit when the map page opens.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,314 questions
{count} votes

1 answer

Sort by: Most helpful
  1. tariqalzubi 1 Reputation point
    2021-03-01T02:41:16.663+00:00

    Hi @Leonard Harris
    If you are using the emulator make sure to choose default location from the options mentioned bellow

    72774-ios.png

    If nothing happens try to change the order like this
    protected override void OnElementChanged(ElementChangedEventArgs<View> e)
    {
    //your code here
    base.OnElementChanged(e);
    }

    0 comments No comments