【问题标题】:How to set background color and change font color on Label in xamarin.forms.maps (iOS)?如何在 xamarin.forms.maps (iOS) 中的 Label 上设置背景颜色和更改字体颜色?
【发布时间】:2021-06-29 07:42:32
【问题描述】:

因为我未能在 iOS 项目的 xamarin.forms.maps 的 pin 上的 Info Window 中再插入两行,所以我设法将它们放在 Info Window 上,如下所示:

在 android 项目中,我有两个扩展名为 .axml 的文件,我在其中添加了两个 <TextView/>,因此我设法在引脚上的 InfoWindow 中添加了另外两行。

但在这里我没有(或不知道在哪里)在 pin 窗口本身中添加另外两个字段,因此以这种方式添加它们:

1.我使用以下代码添加来自数据库的对象:

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 = new CustomMKAnnotationView(annotation, customPin.AlertLevel.ToString());
            annotationView.Image = UIImage.FromFile("pin.png");
            annotationView.CalloutOffset = new CGPoint(0, 0);
            //annotationView.LeftCalloutAccessoryView = new UIImageView(UIImage.FromFile("monkey.png"));
            //annotationView.RightCalloutAccessoryView = UIButton.FromType(UIButtonType.DetailDisclosure);
            ((CustomMKAnnotationView)annotationView).Name = customPin.Name;
            ((CustomMKAnnotationView)annotationView).Url = customPin.Url;
            //Add First Line
            ((CustomMKAnnotationView)annotationView).AlertLevel = customPin.AlertLevel;
            //Add Second Line
            ((CustomMKAnnotationView)annotationView).CodeNum = customPin.CodeNum;
        }
        annotationView.CanShowCallout = true;

        return annotationView;
    }

2。我使用以下代码显示对象:

 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("xamarin.png");
            //customPinView.AddSubview(image);
            customPinView.Center = new CGPoint(0, -(e.View.Frame.Height + 75));

            //the first text to enter in info window
            var labelAlertLevel = new UILabel(new CGRect(60, 0, 200, 84));
            labelAlertLevel.Text = "Код на трвога (1-4): " + customView.AlertLevel.ToString();
            customPinView.AddSubview(labelAlertLevel);

            //the second text to enter in info window
            var labelCodeNum = new UILabel(new CGRect(60, 20, 200, 84));
            labelCodeNum.Text = "Код на станция: " + customView.CodeNum.ToString();
            customPinView.AddSubview(labelCodeNum);
            
            e.View.AddSubview(customPinView);
        }
    }

基本上我想将它们放在 pin 窗口中,但如果这不可能,我该如何设置它们的背景和字体颜色?

我用这个example

【问题讨论】:

    标签: c# ios xamarin maps


    【解决方案1】:

    以下代码包括信息的正常显示、自定义背景颜色和字体颜色,希望对您有所帮助。

    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)
    
                {
    
                    return null;
    
                }
    
                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);
    
                    ((CustomMKAnnotationView)annotationView).Name = customPin.Name;
    
                    ((CustomMKAnnotationView)annotationView).Url = customPin.Url;
    
                    ((CustomMKAnnotationView)annotationView).Description1 = customPin.Description1;
    
                    ((CustomMKAnnotationView)annotationView).Description2 = customPin.Description2;
    
                    ((CustomMKAnnotationView)annotationView).Description3 = customPin.Description3;
    
                    annotationView.CanShowCallout = true;
    
                }
    
                configureDetailView(annotationView);
    
                return annotationView;
    
            }
    
            void configureDetailView(MKAnnotationView annotationView)
    
            {
    
                int width = 100;
    
                int height = 50;
    
                var snapshotView = new UIView();
    
                snapshotView.TranslatesAutoresizingMaskIntoConstraints = false;
    
                NSDictionary views = NSDictionary.FromObjectAndKey(snapshotView, new NSString("snapshotView"));
    
                snapshotView.AddConstraints(NSLayoutConstraint.FromVisualFormat("H:[snapshotView(200)]", new NSLayoutFormatOptions(), null, views));
    
                snapshotView.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:[snapshotView(50)]", new NSLayoutFormatOptions(), null, views));
    
                var options = new MKMapSnapshotOptions();
    
                options.Size = new CGSize(width, height);
    
                options.MapType = MKMapType.SatelliteFlyover;
    
                options.Camera = MKMapCamera.CameraLookingAtCenterCoordinate(annotationView.Annotation.Coordinate, 250, 65, 0);
    
                var snapshotter = new MKMapSnapshotter(options);
    
                snapshotter.Start((snapshot, error) =>
    
                {
    
                    if (snapshot != null)
    
                    {
    
                        UILabel label = new UILabel();
    
                        UILabel label2 = new UILabel();
    
                        UILabel label3 = new UILabel();
    
                        label.Text = ((CustomMKAnnotationView)annotationView).Description1;
    
                        label2.Text = ((CustomMKAnnotationView)annotationView).Description2;
    
                        label3.Text = ((CustomMKAnnotationView)annotationView).Description3;
    
                        label2.BackgroundColor = UIColor.Blue;
    
                        label3.TextColor = UIColor.Red;
    
                        label.Frame = new CGRect(50, 0, 100, 15);
    
                        label2.Frame = new CGRect(50, 15, 100, 15);
    
                        label3.Frame = new CGRect(50, 30, 100, 15);
    
                        snapshotView.AddSubviews(label, label2, label3);
    
                    }
    
                });
                annotationView.DetailCalloutAccessoryView = snapshotView;
            }
    
    
    

    【讨论】:

      猜你喜欢
      • 2020-01-15
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 2011-11-11
      • 1970-01-01
      • 1970-01-01
      • 2014-07-30
      • 2012-08-20
      相关资源
      最近更新 更多