单击Pin时尝试从API加载数据(CustomMap)错误:指定的强制转换无效.-xamarin.forms.maps



当用户点击pin在pin上发送(index+1(作为参数时,我试图将API中的数据加载到自定义映射(xamarin.forms.maps(中。

1.在这里,我在引脚的Address属性上设置了索引+1:

List<CustomPin> pins = new List<CustomPin>();
for (int i = 0; i < HardcodedLocations.Positions.Count; i++)
{
CustomPin pin = new CustomPin
{
Type = PinType.Place,
Position = HardcodedLocations.Positions[i],
Label = "Xamarin San Francisco Office",
Address = $"{i + 1}",
Name = "Xamarin",
Url = "http://xamarin.com/about/"
};
pins.Add(pin);
customMap.Pins.Add(pin);
customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(42.8742, 25.3187), Distance.FromKilometers(250.0)));
}
customMap.CustomPins = pins;

2.在CustomMKAnnotationView类中,我创建属性地址:

public class CustomMKAnnotationView : MKAnnotationView
{
public string Name { get; set; }
public string Url { get; set; }
public string Address { get; set; }
public CustomMKAnnotationView(IMKAnnotation annotation, string id)
: base(annotation, id)
{
}

3.在CustomMapRenderer类I中的GetViewForAnnotation方法中,适当的annotationView.Address在customPin.Address上相等

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.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;
((CustomMKAnnotationView)annotationView).Address = customPin.Address;
}
annotationView.CanShowCallout = true;
return annotationView;
}

4.我在CustomMapRenderer类中创建了一个从API获取数据的方法:

string GenerateRequestUri(string endpoint, string date, string id)
{
string requestUri = endpoint;
requestUri += $"?date={date}";
requestUri += $"&id={id}";
requestUri += $"&daysForward=8";
return requestUri;
}
public async Task<IEnumerable<AladinModel>> GetDataFromAPI(string indexOnClick)
{
DateTime dtNow = DateTime.Now;
var dtNowAPI = dtNow.ToString("yyyy-MM-dd");
var listData = new List<AladinModel>();
var result = await _restServiceAPI.GetAladinData(GenerateRequestUri(ConstantsAPI.EndPoint, dtNowAPI, indexOnClick));
foreach (var item in result)
{
var currentData = new AladinModel()
{

Dats = item.Dats,
Ta = item.Ta,
Rh = item.Rh,
Ws = item.Ws,
Rr = item.Rr,
Sr = item.Sr,
Apres = item.Apres
};
listData.Add(currentData);
}
return listData;
}

5.在OnDidSelectAnnotationView方法中,我试图用MessagingCenter发送数据:

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));
e.View.AddSubview(customPinView);
}
string addressIndex = customView.Address;
var result = GetDataFromAPI(addressIndex);
MessagingCenter.Send<object, IEnumerable<AladinModel>>(this, "PinSelected", (IEnumerable<AladinModel>)result);
}

6.在主页中,我试图接收这样的数据:

protected override void OnAppearing()
{
base.OnAppearing();
MarkerPressed();
}
public void MarkerPressed()
{
MessagingCenter.Subscribe<object, IEnumerable<AladinModel>>(this, "PinSelected", (sender, arg) =>
{
var test = arg;
});
}

当我点击标记时,我收到错误:指定的投射在此行无效:

MessagingCenter.Send<object, IEnumerable<AladinModel>>(this, "PinSelected", (IEnumerable<AladinModel>)result);

Тhis是我的对象类:

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace pizhevsoft.Models
{
public class ItemsAPI
{
public partial class RootAladinModel
{
[JsonProperty("aladinModel")]
public AladinModel[] AladinModel { get; set; }
}
public partial class AladinModel
{
[JsonProperty("DATS")]
public DateTime Dats { get; set; }
[JsonProperty("TA")]
public double Ta { get; set; }
[JsonProperty("RH")]
public double Rh { get; set; }
[JsonProperty("WS")]
public double Ws { get; set; }
[JsonProperty("RR")]
public double Rr { get; set; }
[JsonProperty("SR")]
public double Sr { get; set; }
[JsonProperty("APRES")]
public double Apres { get; set; }
}
}
}

单击标记时的主要目标是获取其索引+1,将其作为参数传递给API以获取数据?

如果有更简单的选择或方法来处理这个问题,请分享如何做?

根据我的逻辑,必须在CustomMapRenderer类中创建一个方法GetDataFromAPI,以便在OnDidSelectAnnotationView中调用,并将数据发送到带有消息中心的Main项目?

GetDataFromAPI异步方法,需要使用等待调用

最新更新