【问题标题】:Multiple labels inside tooltip of the pin marker with click events in Xamarin.Forms.GoogleMaps在 Xamarin.Forms.GoogleMaps 中带有点击事件的 pin 标记工具提示内的多个标签
【发布时间】:2021-09-06 16:58:52
【问题描述】:

我正在使用 XamarinForms.GoogleMaps。是否可以在 Pin 的工具提示内显示堆叠的多个标签?我想通过单击每个标签来执行某些操作。 我看到我只能将标签文本传递给Label属性

  Pins.Add(new Pin
                    {
                        Type = PinType.Place,
                        Address = ””,
                        Label = “label text goes here”,
                        Position = new Position(Convert.ToDouble(obj.GeoLat),
                            Convert.ToDouble(obj.GeoLng))
                    });

要求输出应该是这样的:

【问题讨论】:

    标签: ios xamarin xamarin.forms xamarin.ios


    【解决方案1】:
    1. CustomPin 类中添加新属性。
    public string[] Details { get; set; }
    
    1. 赋值。
    
    CustomPin pin = new CustomPin
    {
        Type = PinType.Place,
        Position = new Position(37.79752, -122.40183),
        Label = "Test",
        Address = "394 Pacific Ave, San Francisco CA",
        Name = "Xamarin",
        Url = "http://xamarin.com/about/",
    
        Details = new string[] {"111","222","333"}  //this line
    };
    
    1. 在自定义渲染器中构建界面。
     void OnDidSelectAnnotationView(object sender, MKAnnotationViewEventArgs e)
            {
                CustomMKAnnotationView customView = e.View as CustomMKAnnotationView;
                CustomPin pin = GetCustomPin(customView.Annotation as MKPointAnnotation);
    
                customPinView = new UIView();
                customPinView.BackgroundColor = UIColor.Red;
                if (customView.Name.Equals("Xamarin"))
                {
    
                    int index = 0;
                    foreach(var item in pin.Details)
                    {
                        UILabel label = new UILabel(new CGRect(0, index * 30, 100, 20));
                        label.BackgroundColor = UIColor.White;
                        label.TextAlignment = UITextAlignment.Center;
                        label.Text = item;
                        customPinView.Add(label);
                        index++;
                    }
                    customPinView.Frame = new CGRect(0, 0, 100, index*30);
                    customPinView.Center = new CGPoint(0, -(e.View.Frame.Height + 75));
                    e.View.AddSubview(customPinView);
                }
            }
    

    更新

    如果你使用 XamarinForm.GoogleMapsPin类是密封类所以我们不能创建子类,但是我们可以用Tag 属性设置值,参考here

    //xaml
    
    pin.Tag = new string[] {"111","222","333"};
    
    //custom renderer
     string[] details = pin.Tag as string[];
     foreach(var item in details)
     {
          UILabel label = new UILabel(new CGRect(0, index * 30, 100, 20));
          label.BackgroundColor = UIColor.White;
          label.TextAlignment = UITextAlignment.Center;
          label.Text = item;
          customPinView.Add(label);
          index++;
     }
    

    【讨论】:

    • 我正在使用 XamarinForm.GoogleMaps,因此我无法继承 Pin 类来为其添加自定义属性。
    • 请检查我的更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    • 2011-04-09
    • 2018-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多