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

皮热夫赛车

因为我没有xamarin.forms.maps在 iOS 项目上的引脚上的信息窗口中再插入两行,所以我设法将它们放在信息窗口上,如下所示:

截屏

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

我用这个例子

利尔

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

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


在此处输入图片说明

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 ios 中设置 Xamarin.forms 标签页未选中的图标和文本颜色

如何在 iOS 项目上使用 xamarin.forms.maps 从 CustomMapRenderer 中的其他方法调用对象

如何在 IOS Xamarin.Forms 中更改浮动 TextField 默认占位符颜色

Xamarin.Forms.Maps iOS填充

如何在Xamarin Forms for iOS中创建导航栏?

如何在 Xamarin Forms for iOS 中实现长按?

Xamarin Forms:如何在 Plugin.MediaManager.Forms 中播放音频时更改背景颜色?

如何更改 Xamarin Forms 中 `ListView` 内 `TextCell` 的背景颜色?

如何在 Xamarin.Forms 中设置 ContentPresenter 的文本颜色?

如何在Xamarin.Forms中更改输入的边框颜色

如何在 CustomMapRenderer - xamarin.forms.maps 中将同步方法更改为异步方法?

如何在 Android 中使用图标更改 xamarin.forms.maps 标记

如何在 Xamarin.Forms.Maps 中自定义 MkMapView 标注/视图

Xamarin Forms-颜色如何在iOS的导航栏上工作

Xamarin Forms:如何在ListView内绑定Xamarin.forms.Maps的位置值?

Xamarin.Forms中的异步任务如何在iOS / Android背景模式下工作

如何更改导航Titleview-Xamarin Forms的背景颜色?

如何在 Xamarin.Forms 中获取 iOS 和 Android 的辅助功能字体大小,以便我可以在 HTML WebView 中更改字体大小?

如何在Xamarin iOS中更改选项卡式页面的背景颜色?

如何在Xamarin.forms(适用于Android和iOS)中访问通讯录?

Xamarin Forms 如何在 Android 和 iOS 的 XAML 文件中调用不同的渲染器

如何在 xamarin.forms 中为 ios 和 android 编写不同的代码?

如何在 iOS 和 Xamarin 中更改导航栏 < 和单词的颜色?

如何用很多引脚填充 xamarin.forms.maps - Xamarin

如何在Xamarin Forms上将PowerOn更改为iOS上的蓝牙状态?

Xamarin.Forms如何更改iOS上的默认蓝色

Xamarin.Forms-如何在iOS和Android上使导航/系统栏透明

如何在Android和iOS(Xamarin Forms)上获取键盘高度?

如何在Xamarin.Forms中允许iOS状态栏和iPhone X缺口