我想在點擊大頭針時更改大頭針圖示。
我從我的GetViewForAnnotation方法中復制了這段代碼,OnDidSelectAnnotationView但我不知道如何回傳 annotationView.
我不確定這是否是正確的方法,但是我可以舉個例子說明當我點擊標記時如何更改圖示嗎?
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);
customPinView.Center = new CGPoint(0, -(e.View.Frame.Height 75));
e.View.AddSubview(customPinView);
}
var pin = GetCustomPin(c_annotation as MKPointAnnotation);
int mapCode = customView.MapCode;
var result = GetDataFromAPI(mapCode);
var result2 = GetInfoFromStation(mapCode);
MessagingCenter.Send<object, IEnumerable<AlertLevel>>(this, "PinSelected", result);
MessagingCenter.Send<object, IEnumerable<StationInfoOnClick>>(this, "StationInfo", result2);
MKAnnotationView annotationView = null;
if (annotationView == null)
{
annotationView = new CustomMKAnnotationView(c_annotation, pin.Name);
annotationView.CalloutOffset = new CGPoint(0, 0);
((CustomMKAnnotationView)annotationView).Name = pin.Name;
((CustomMKAnnotationView)annotationView).Url = pin.Url;
((CustomMKAnnotationView)annotationView).Address = pin.Address;
//Add First Line
((CustomMKAnnotationView)annotationView).AlertLevel = pin.AlertLevel;
if (pin.AlertLevel == 1)
{
annotationView.Image = UIImage.FromFile("red.png");
}
else if (pin.AlertLevel == 2)
{
annotationView.Image = UIImage.FromFile("yellow.png");
}
else if (pin.AlertLevel == 3)
{
annotationView.Image = UIImage.FromFile("orange.png");
}
else if (pin.AlertLevel == 4)
{
annotationView.Image = UIImage.FromFile("red.png");
}
//Add Second Line
((CustomMKAnnotationView)annotationView).CodeNum = pin.CodeNum;
((CustomMKAnnotationView)annotationView).MapCode = pin.MapCode;
}
annotationView.CanShowCallout = true;
configureDetailView(annotationView);
return annotationView;
}
我試圖改變void OnDidSelectAnnotationView與MKAnnotationView OnDidSelectAnnotationView我收到這個方法錯誤:
protected override void OnElementChanged(ElementChangedEventArgs<View> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
var nativeMap = Control as MKMapView;
nativeMap.GetViewForAnnotation = null;
nativeMap.CalloutAccessoryControlTapped -= OnCalloutAccessoryControlTapped;
nativeMap.DidSelectAnnotationView -= OnDidSelectAnnotationView;
nativeMap.DidDeselectAnnotationView -= OnDidDeselectAnnotationView;
}
if (e.NewElement != null)
{
var formsMap = (CustomMap)e.NewElement;
var nativeMap = Control as MKMapView;
customPins = formsMap.CustomPins;
nativeMap.GetViewForAnnotation = GetViewForAnnotation;
nativeMap.CalloutAccessoryControlTapped = OnCalloutAccessoryControlTapped;
nativeMap.DidSelectAnnotationView = OnDidSelectAnnotationView;
nativeMap.DidDeselectAnnotationView = OnDidDeselectAnnotationView;
}
}
錯誤是: Error CS0407: 'MKAnnotationView CustomMapRenderer.OnDidSelectAnnotationView(object, MKAnnotationViewEventArgs)' has the wrong return type (CS0407) (MaritsaTundzhaForecast.iOS)
單擊時是否有另一種方法可以更改地圖示記圖示?
這是我的GetViewForAnnotation方法:
protected override MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
{
//от mainpage извикваш метода
MKAnnotationView annotationView = null;
if (annotation is MKUserLocation)
return null;
var customPin = GetCustomPin(annotation as MKPointAnnotation);
//Get Value
c_annotation = annotation;
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;
((CustomMKAnnotationView)annotationView).MapCode = customPin.MapCode;
}
annotationView.CanShowCallout = true;
configureDetailView(annotationView);
return annotationView;
}
我想單擊以放置具有不同大小的相同圖示。
我用這個

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/325229.html
標籤:C# 沙马林 xamarin.forms
上一篇:Redis的安裝使用
