如何在xamarin.forms.maps (iOS) 中设置Label 的背景颜色和更改字体颜色?

How to set background color and change font color on Label in xamarin.forms.maps (iOS)?

因为我未能在 iOS 项目 xamarin.forms.maps 的引脚上的 Info Window 中再插入两行,所以我设法将它们放在 Info Window 上,就像这样:

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

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

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 中 window,但如果这不可能,我该如何设置它们的背景和字体颜色?

我用这个example.

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

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;
        }