當用戶單擊引腳以像引數一樣在引腳上發送(索引 1)時,我正在嘗試從自定義地圖(xamarin.forms.maps)中的 API 加載資料。
1. 在這里,我在 pin 的 Address 屬性上設定 index 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 類的 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. 在 MainPage 我試圖接收這樣的資料:
protected override void OnAppearing()
{
base.OnAppearing();
MarkerPressed();
}
public void MarkerPressed()
{
MessagingCenter.Subscribe<object, IEnumerable<AladinModel>>(this, "PinSelected", (sender, arg) =>
{
var test = arg;
});
}
當我單擊標記時,我收到錯誤:Specified cast is not valid on this line:
MessagingCenter.Send<object, IEnumerable<AladinModel>>(this, "PinSelected", (IEnumerable<AladinModel>)result);
Т這是我的物件類:
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 中呼叫,并將資料發送到帶有訊息中心的主專案?
uj5u.com熱心網友回復:
GetDataFromAPI是一個異步方法,需要使用await呼叫
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/451006.html
