【发布时间】:2019-02-10 09:39:19
【问题描述】:
我正在学习如何在 Xamarin 中做列表,我昨天开始了,我有工作要在接下来的几天内完成。我的第一个应用程序运行良好,但第二个应用程序(主要基于第一个应用程序,我有一门课时长为 1:30 并且没有学到很多东西),当我在 Android 上运行我的新程序时,我得到了无法反序列化的错误。
我很确定我的错误在 MainPage.xaml.cs 中
meteo0.cs(第 8 到 26 行)
public class Meteo0
{
public string precipitaProb { get; set; }
public int tMin { get; set; }
public int tMax { get; set; }
public string predWindDir { get; set; }
public int idWeatherType { get; set; }
public int classWindSpeed { get; set; }
public string longitude { get; set; }
public int classPrecInt { get; set; }
public int globalIdLocal { get; set; }
public string latitude { get; set; }
public string owner { get; set; }
public string country { get; set; }
public string forecastDate { get; set; }
public List<Meteo0> data { get; set; }
public DateTime dataUpdate { get; set; }
}
MainPage.xaml.cs(第 10 到 39 行)
public partial class MainPage : ContentPage
{
const string Url = "https://api.ipma.pt/open-data/forecast/meteorology/cities/daily/1110600";
public MainPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
GetMeteo0();
base.OnAppearing();
}
async void GetMeteo0()
{
List<Meteo0> meteo0;
HttpClient httpClient = new HttpClient();
var content = await httpClient.GetStringAsync(Url);
meteo0 = JsonConvert.DeserializeObject<List<Meteo0>>(content);
meteo0ListView.ItemsSource = meteo0;
}
private void meteo0ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
Meteo0 meteo0 = e.SelectedItem as Meteo0;
Navigation.PushAsync(new MeteoDetails(meteo0));
}
}
编辑的 MainPage.xaml.cs
public partial class MainPage : ContentPage
{
const string Url = "https://api.ipma.pt/open-data/forecast/meteorology/cities/daily/1110600";
public MainPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
GetMeteo0();
base.OnAppearing();
}
async void GetMeteo0()
{
List<Meteo0> meteo0;
HttpClient httpClient = new HttpClient();
var content = await httpClient.GetStringAsync(Url);
var result = JsonConvert.DeserializeObject<RootObject>(content);
meteo0ListView.ItemsSource = result.data;
}
private void meteo0ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
Meteo0 meteo0 = e.SelectedItem as Meteo0;
Navigation.PushAsync(new MeteoDetails(meteo0));
}
}
编辑过的meteo0.cs
public class Meteo0
{
public string precipitaProb { get; set; }
public double tMin { get; set; }
public double tMax { get; set; }
public string predWindDir { get; set; }
public int idWeatherType { get; set; }
public int classWindSpeed { get; set; }
public string longitude { get; set; }
public int classPrecInt { get; set; }
public int globalIdLocal { get; set; }
public string latitude { get; set; }
}
新的 RootObject.cs
public class RootObject
{
public string owner { get; set; }
public string country { get; set; }
public string forecastDate { get; set; }
public List<Meteo0> data { get; set; }
public DateTime dataUpdate { get; set; }
}
MainPage.xaml
<ListView x:Name="meteo0ListView" ItemSelected="meteo0ListView_ItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="Weather"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
【问题讨论】:
-
实际的错误信息和堆栈跟踪是什么?
-
<TextCell Text="Weather"/>这是您的数据绑定吗?如果是,你应该使用<TextCell Text="{Binding property}"/>
标签: c# json xamarin xamarin.forms