在iOS上渲染后如何保留人针事件

时间:2019-01-24 14:33:21

标签: xamarin xamarin.forms xamarin.ios

Xamarin引入了一个自定义地图图钉的示例

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/map/customized-pin

在此示例中,我尝试以这种方式使用pin的原始事件(在Xamarin Form中定义)

public MapPage()
{
    InitializeComponent();

    var pin = new CustomPin
    {
        Type = PinType.Place,
        Position = new Position(37.79752, -122.40183),
        Label = "Xamarin San Francisco Office",
        Address = "394 Pacific Ave, San Francisco CA",
        Id = "Xamarin",
        Url = "http://xamarin.com/about/"
    };
    pin.Clicked += Pin_Clicked;
    customMap.CustomPins = new List<CustomPin> { pin };
    customMap.Pins.Add(pin);
    customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(37.79752, -122.40183), Distance.FromMiles(1.0)));
}

private async void Pin_Clicked(object sender, System.EventArgs e)
{
    await DisplayAlert("Alert", ((CustomPin)sender).Label, "OK");
}

对于Android,我在OnElementChanged中注释以下行 NativeMap.InfoWindowClick-= OnInfoWindowClick; 和 NativeMap.InfoWindowClick + = OnInfoWindowClick; 在OnMapReady中

protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Map> e)
{
    base.OnElementChanged(e);

    if (e.OldElement != null)
    {
        //NativeMap.InfoWindowClick -= OnInfoWindowClick;
    }

    if (e.NewElement != null)
    {
        var formsMap = (CustomMap)e.NewElement;
        customPins = formsMap.CustomPins;
        Control.GetMapAsync(this);
    }
}

protected override void OnMapReady(GoogleMap map)
{
    base.OnMapReady(map);

    //NativeMap.InfoWindowClick += OnInfoWindowClick;
    NativeMap.SetInfoWindowAdapter(this);
}

点击图钉的信息窗口后,可以正确触发图钉事件

但是在iOS设备上,我在OnElementChanged中注释掉了nativeMap.CalloutAccessoryControlTapped之后。无法触发该事件:

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

    if (e.OldElement != null)
    {
        var nativeMap = Control as MKMapView;
        nativeMap.GetViewForAnnotation = null;
        //nativeMap.CalloutAccessoryControlTapped -= OnCalloutAccessoryControlTapped;
        nativeMap.DidSelectAnnotationView -= OnDidSelectAnnotationView;
        nativeMap.DidDeselectAnnotationView -= OnDidDeselectAnnotationView;
    }

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

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

您是否有解决iOS上的固定事件故障的想法?

0 个答案:

没有答案