Xamarin 休息 api
Xamarin rest api
绑定在我的代码中不起作用。这段代码有什么问题?
HttpClient client = new HttpClient();
var response = await client.GetAsync(string.Format("uri link"));
string jsonstring = await response.Content.ReadAsStringAsync();
RootObject item = JsonConvert.DeserializeObject<RootObject>(jsonstring);
titles.ItemsSource =item.ToString();
XAML代码
<ListView x:Name="titles" HasUnevenRows="False" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding note}"/>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
class 对象:
public class Result
{
public string note { get; set; }
}
public class Response
{
public List<Result> results { get; set; }
}
public class RootObject
{
public Response response { get; set; }
}
您将标签绑定到注释,但将 titles.ItemsSource 设置为 RootObject。 RootObject class 没有注释。注意在结果 class 中。
而且您不能那样设置项目源。
我建议你这样做
var listItem = JsonConvert.DeserializeObject<List<Result>>(jsonstring);
titles.ItemsSource = l;
据我所知:
RootObject item = JsonConvert.DeserializeObject<RootObject>(jsonstring);
你能不能在上面一行之后试试这个代码:
titles.ItemsSource =item.Responce. results;
绑定在我的代码中不起作用。这段代码有什么问题?
HttpClient client = new HttpClient();
var response = await client.GetAsync(string.Format("uri link"));
string jsonstring = await response.Content.ReadAsStringAsync();
RootObject item = JsonConvert.DeserializeObject<RootObject>(jsonstring);
titles.ItemsSource =item.ToString();
XAML代码
<ListView x:Name="titles" HasUnevenRows="False" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding note}"/>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
class 对象:
public class Result
{
public string note { get; set; }
}
public class Response
{
public List<Result> results { get; set; }
}
public class RootObject
{
public Response response { get; set; }
}
您将标签绑定到注释,但将 titles.ItemsSource 设置为 RootObject。 RootObject class 没有注释。注意在结果 class 中。
而且您不能那样设置项目源。
我建议你这样做
var listItem = JsonConvert.DeserializeObject<List<Result>>(jsonstring);
titles.ItemsSource = l;
据我所知:
RootObject item = JsonConvert.DeserializeObject<RootObject>(jsonstring);
你能不能在上面一行之后试试这个代码:
titles.ItemsSource =item.Responce. results;