- 在
CustomPin 类中添加新属性。
public string[] Details { get; set; }
- 赋值。
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
};
- 在自定义渲染器中构建界面。
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.GoogleMaps,Pin类是密封类所以我们不能创建子类,但是我们可以用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++;
}