如何解决如何使用 xamarin.forms.maps 从一种方法中获取对象并像参数一样传递给另一种方法 - iOS
我尝试通过此方法在查询时传递类似 CodeNum
对象的参数:
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.CalloutOffset = new CGPoint(0,0);
((CustomMKAnnotationView)annotationView).Name = customPin.Name;
((CustomMKAnnotationView)annotationView).Url = customPin.Url;
((CustomMKAnnotationView)annotationView).Address = customPin.Address;
//Add First Line
((CustomMKAnnotationView)annotationView).AlertLevel = customPin.AlertLevel;
if (customPin.AlertLevel == 1)
{
annotationView.Image = UIImage.FromFile("green.png");
}
else if (customPin.AlertLevel == 2)
{
annotationView.Image = UIImage.FromFile("yellow.png");
}
else if (customPin.AlertLevel == 3)
{
annotationView.Image = UIImage.FromFile("orange.png");
}
else if (customPin.AlertLevel == 4)
{
annotationView.Image = UIImage.FromFile("red.png");
}
//Add Second Line
((CustomMKAnnotationView)annotationView).CodeNum = customPin.CodeNum;
}
annotationView.CanShowCallout = true;
configureDetailView(annotationView);
return annotationView;
}
当用户点击地图上的某个图钉以获取 CodeNum
并传递给查询以从数据库中获取数据时。如何将此参数传递给 OnDidSelectAnnotationView
方法?
void OnDidSelectAnnotationView(object sender,MKAnnotationViewEventArgs e)
{
var customPin = GetCustomPin(annotation as MKPointAnnotation);
var result = DataBaseConnection(customPin.CodeNum);
MessagingCenter.Send<object,IEnumerable<AlertLevel>>(this,"PinSelected",result);
CustomMKAnnotationView customView = e.View as CustomMKAnnotationView;
customPinView = new UIView();
if (customView.Name.Equals("Xamarin"))
{
customPinView.Frame = new CGRect(0,200,84);
customPinView.Center = new CGPoint(0,-(e.View.Frame.Height + 75));
e.View.AddSubview(customPinView);
}
}
在 OnDidSelectAnnotationView
方法中,这行代码出现错误:
var customPin = GetCustomPin(annotation as MKPointAnnotation);
错误 CS0103:名称“注释”在当前上下文中不存在 (CS0103)
我的 GetCustomPin
方法如下所示:
CustomPin GetCustomPin(MKPointAnnotation annotation)
{
var position = new Position(annotation.Coordinate.Latitude,annotation.Coordinate.Longitude);
foreach (var pin in customPins)
{
if (pin.Position == position)
{
return pin;
}
}
return null;
}
public IEnumerable<AlertLevel> DataBaseConnection(int mapCode)
{
string ConnectionString = "server=192.168.1.2;uid=UName;port=4443;pwd=Password;database=dbname;";
MysqLConnection Conn = new MysqLConnection(ConnectionString);
var listAlert = new List<AlertLevel>();
try
{
Conn.open();
//replace(2) with mapCode
string query = "CALL Get_Alert_levels_Station(" + mapCode + ");";
MysqLCommand myCommand = new MysqLCommand(query,Conn);
MysqLDataReader myReader;
myReader = myCommand.ExecuteReader();
try
{
while (myReader.Read())
{
var currentData = new AlertLevel()
{
dateForecast = myReader.GetDateTime(0),levelForecast = myReader.GetInt32(1)
};
listAlert.Add(currentData);
}
}
finally
{
myReader.Close();
Conn.Close();
}
}
catch (Exception ex)
{
Console.WriteLine("Database Connection","Not Connected ..." + Environment.NewLine + ex.ToString(),"OK");
}
return listAlert;
}
如何从点击的 pin 中获取 CodeNum 并像变量 DataBaseConnection
一样传递给 mapCode
方法?
解决方法
可以使用 MessagingCenter 发送消息 您可以通过以下链接使用 MessagingCenter MessagingCenter
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。