如何获取 JSON 对象并将其设置为列表视图,然后再在 Xamarin 窗体中设置列表视图



移动到新页面后,我想自动设置从主机获取的JSON数据并将其放入ListView中。实际上,ListView是在下载JSON之前设置的。我正在寻找在 JSON 反序列化完成后设置 ListView 的解决方案。这是我的代码。

public async void DownloadMapsFromServer()
{
HttpClientHandler clientHandler = new HttpClientHandler();
clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
HttpClient client = new HttpClient(clientHandler);
var content = await client.GetStringAsync("https://10.0.2.2:44353/v1/RaceMaps/");
var jsonlist = new List<RaceMapBindingModel>();
jsonlist = JsonConvert.DeserializeObject<List<RaceMapBindingModel>>(content);
}
public MapsList()
{
DownloadMapsFromServer();
Label header = new Label
{
Text = "ListView",
FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
HorizontalOptions = LayoutOptions.Center
};
// Define some data.
List<MapsListView> Maps = new List<MapsListView>();
foreach (RaceMapBindingModel item in jsonlist)
{
Maps.Add(new MapsListView($"{item.Name}", $"{item.Description}", Color.Red));
}

我找到了答案。我没有使用 HttpClient.GetAsync,而是使用了 WebClient.DownloadString 方法。

最新更新