【发布时间】: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。
【问题讨论】: