首页 > 解决方案 > Xamarin Forms 4.1 CalloutAccessoryControlTapped 不再需要点击注释视图

问题描述

最近,iOS 地图注释中的一个行为发生了变化:

在此处输入图像描述

CalloutAccessoryControlTapped当用户点击注释视图时,不再调用该事件。例如,如果我点击上图中的红色区域。触发事件的唯一方法是点击右侧的信息按钮。

CalloutAccessoryControlTapped当我们点击注释的整个表面时,有没有办法强制提出?

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

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

                if (e.NewElement != null)
                {
                    CustomMap = (CustomMap)e.NewElement;

                    nativeMap.GetViewForAnnotation = GetViewForAnnotation;
                    nativeMap.CalloutAccessoryControlTapped += OnCalloutAccessoryControlTapped;
                }
            }
        }

// event delegate
private void OnCalloutAccessoryControlTapped(object sender, MKMapViewAccessoryTappedEventArgs e)
    {
        // ...
    }

标签: xamarin.iosxamarin.forms.maps

解决方案


您可以在其中添加自定义aUITapGestureRecognizerView达到效果,以下是步骤:

首先,ges在 中定义 a CustomMapRenderer

public class CustomMapRenderer : MapRenderer
{
    UIView customPinView;
    List<CustomPin> customPins;

    UITapGestureRecognizer ges;

    ...
}

然后在 中OnDidSelectAnnotationView,添加gescustomView

void OnDidSelectAnnotationView(object sender, MKAnnotationViewEventArgs e)
{
    var customView = e.View as CustomMKAnnotationView;
    customPinView = new UIView();


    Action action = () => {

        if (!string.IsNullOrWhiteSpace(((CustomMKAnnotationView)customView).Url))
        {
            UIApplication.SharedApplication.OpenUrl(new Foundation.NSUrl(((CustomMKAnnotationView)customView).Url));
        }
    };

    ges = new UITapGestureRecognizer(action);
    customView.AddGestureRecognizer(ges);

    if (customView.MarkerId == "Xamarin")
    {
        customPinView.Frame = new CGRect(0, 0, 200, 84);
        var image = new UIImageView(new CGRect(0, 0, 200, 84));
        image.Image = UIImage.FromFile("xamarin.png");
        customPinView.AddSubview(image);
        customPinView.Center = new CGPoint(0, -(e.View.Frame.Height + 75));
        e.View.AddSubview(customPinView);
    }
}

并将其删除OnDidDeselectAnnotationView

void OnDidDeselectAnnotationView(object sender, MKAnnotationViewEventArgs e)
{

    var customView = e.View as CustomMKAnnotationView;
    customView.RemoveGestureRecognizer(ges);

    if (!e.View.Selected)
    {
        customPinView.RemoveFromSuperview();
        customPinView.Dispose();
        customPinView = null;
    }
}

参考:定制销


推荐阅读