为什么我无法在 iOS 上的地图图钉中添加两个字段 - xamarin

本·约翰逊

CustomMapRenderer在 iOS 项目上有 e ,我想在标记单击中再添加两个字段。

CustomMKAnnotationView.cs我创建了另外两个对象 -CodeNumAlertLevel

    using MapKit;

namespace MaritsaTundzhaForecast.iOS
{
    public class CustomMKAnnotationView : MKAnnotationView
    {
        public string Name { get; set; }

        public string Url { get; set; }

        public int AlertLevel { get; set; }

        public int CodeNum { get; set; }

        public CustomMKAnnotationView(IMKAnnotation annotation, string id)
            : base(annotation, id)
        {
        }
    }
}

CustomMapRenderer.cs我使用这行代码来显示它但是当我点击 pin 时它们不会出现:

((CustomMKAnnotationView)annotationView).AlertLevel = customPin.AlertLevel;
((CustomMKAnnotationView)annotationView).CodeNum = customPin.CodeNum;

这是完整的代码GetViewForAnnotation

 protected override MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
    {
        MKAnnotationView annotationView = null;

        if (annotation is MKUserLocation)
            return null;

        var customPin = GetCustomPin(annotation as MKPointAnnotation);
        if (customPin == null)
        {
            throw new Exception("Custom pin not found");
        }

        annotationView = mapView.DequeueReusableAnnotation(customPin.Name);
        if (annotationView == null)
        {
            annotationView = new CustomMKAnnotationView(annotation, customPin.Name);
            annotationView.Image = UIImage.FromFile("pin.png");
            annotationView.CalloutOffset = new CGPoint(0, 0);
            annotationView.LeftCalloutAccessoryView = new UIImageView(UIImage.FromFile("green.png"));
            
            ((CustomMKAnnotationView)annotationView).Name = customPin.Name;
            ((CustomMKAnnotationView)annotationView).Url = customPin.Url;
            ((CustomMKAnnotationView)annotationView).AlertLevel = customPin.AlertLevel;
            ((CustomMKAnnotationView)annotationView).CodeNum = customPin.CodeNum;
        }
        annotationView.CanShowCallout = true;

        return annotationView;
    }

我有OnDidSelectAnnotation方法,但我不知道在里面写什么来显示CodeNumAlertLevel

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

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

    void OnDidDeselectAnnotationView(object sender, MKAnnotationViewEventArgs e)
    {
        if (!e.View.Selected)
        {
            customPinView.RemoveFromSuperview();
            customPinView.Dispose();
            customPinView = null;
        }
    }
杰森

您需要修改 UI 以显示附加数据

// you will need to experiment with the Bounds to fit your UI
var label = new UILabel(new CGRect(0, 0, 100, 50));
label.Text = customView.AlertLevel;
customPinView.AddSubview(label);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我无法在iOS中将字典中的值附加到数组?

为什么我无法将APNs Development iOS类型的证书添加到配置文件中

为什么Xamarin突然不让我部署到iOS设备

为什么LambdaExpression.Compile()在iOS(Xamarin)上可以使用?

为什么在我的Swift ios应用程序中无法单击UICollectionView单元格?

为什么我不能使用iOS Designer创建序列?-Xamarin Studio

Xamarin iOS:无法在Visual Studio 2017的控件上设置约束

在Xamarin.Forms中订阅事件时,为什么只能在iOS上收到NullReferenceException?

无法在iOS上使用Xamarin和MvvmCross拍照

Xamarin Forms:无法在设备上启动ios应用程序

为什么在iOS 13中没有调用OpenUrl()但在iOS 12中却调用了OpenUrl()。(Xamarin.ios)

为什么使用Xamarin Forms的iOS应用程序无法显示“真棒字体”?

Xamarin iOS:无法在Visual Studio 2013中渲染iOS Storyboard

为什么collectionViewCells无法在iOS中显示?

无法在Xamarin Studio Mac中的iOS Simulator上启动应用

无法在Xamarin.IOS中查看PushViewController

Xamarin中的iOS上的handlebars.net无法正常工作

为什么我的ios 10中的uilabel无法扩展?

Xamarin iOS Designer 无法添加自适应字体大小

为什么在我的 Xamarin.Forms 应用程序的 iOS 版本的某些屏幕中禁用条目和选择器?

为什么 xamarin.ios 中的启动屏幕周围有这么多混乱

为什么我无法访问表单后面代码中的 xamarin 元素

为什么 Xamarin.iOS 中的 AppDelegate 类是分部类?

为什么 Xamarin Forms Webview 绑定不会在 iOS 上刷新?

为什么此任务等待离开 Xamarin iOS 中的 UI 上下文?

为什么 Xamarin.Android 上的 JSON.NET 永远无法完成解析,我该如何解决?

为什么在 xamarin 形式的写入操作后,我会在 iOS 中收到 BLE 的配对请求对话框?

为什么我在 iOS 的 PhoneDialer.Open 中收到 FeatureNotSupported 异常,但在 Xamarin Forms 的 Android 中工作正常?

Xamarin IOS 中的 UILongPressGestureRecognizer