Trying to load data from API when Pin is clicked (CustomMap) error: Specified cast is not valid. - xamarin.forms.maps

Trying to load data from API when Pin is clicked (CustomMap) error: Specified cast is not valid. - xamarin.forms.maps

我正在尝试从自定义地图 (xamarin.forms.maps) 中的 API 加载数据,当用户点击 pin 发送 (index + 1) 作为参数时。

1.在这里,我在地址 属性 上设置索引 + 1 在 pin:

         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 class 我创建 属性 地址:

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 class 的 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 class:

中创建了一个从 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.在 MainPage 中,我尝试接收这样的数据:

        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);

T这是我的对象class:

    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 class 中创建方法 GetDataFromAPI,以便在 OnDidSelectAnnotationView 中调用,并将数据发送到带有消息中心的主项目?

GetDataFromAPI 是一个 async 方法,需要使用 await

调用