Xamarin ListView不显示来自api的json结果



与title相同,我的listview没有显示任何内容,我认为我在该应用程序中的模型不好帮助在jsonObject中取消序列化json,然后创建一个objectModel列表,但不显示我感谢

public class Marvel
{
}
public class TextObject
{
public string type { get; set; }
public string language { get; set; }
public string text { get; set; }
}
public class Url
{
public string type { get; set; }
public string url { get; set; }
}
public class Series
{
public string resourceURI { get; set; }
public string name { get; set; }
}
public class Variant
{
public string resourceURI { get; set; }
public string name { get; set; }
}
public class Date
{
public string type { get; set; }
public DateTime date { get; set; }
}
public class Price
{
public string type { get; set; }
public double price { get; set; }
}
public class Thumbnail
{
public string path { get; set; }
public string extension { get; set; }
}
public class Image
{
public string path { get; set; }
public string extension { get; set; }
}
public class Item
{
public string resourceURI { get; set; }
public string name { get; set; }
public string role { get; set; }
public string type { get; set; }
}
public class Creators
{
public int available { get; set; }
public string collectionURI { get; set; }
public List<Item> items { get; set; }
public int returned { get; set; }
}
public class Characters
{
public int available { get; set; }
public string collectionURI { get; set; }
public List<Item> items { get; set; }
public int returned { get; set; }
}
public class Stories
{
public int available { get; set; }
public string collectionURI { get; set; }
public List<Item> items { get; set; }
public int returned { get; set; }
}
public class Events
{
public int available { get; set; }
public string collectionURI { get; set; }
public List<object> items { get; set; }
public int returned { get; set; }
}
public class Result
{
public int id { get; set; }
public int digitalId { get; set; }
public string title { get; set; }
public int issueNumber { get; set; }
public string variantDescription { get; set; }
public string description { get; set; }
public DateTime modified { get; set; }
public string isbn { get; set; }
public string upc { get; set; }
public string diamondCode { get; set; }
public string ean { get; set; }
public string issn { get; set; }
public string format { get; set; }
public int pageCount { get; set; }
public List<TextObject> textObjects { get; set; }
public string resourceURI { get; set; }
public List<Url> urls { get; set; }
public Series series { get; set; }
public List<Variant> variants { get; set; }
public List<object> collections { get; set; }
public List<object> collectedIssues { get; set; }
public List<Date> dates { get; set; }
public List<Price> prices { get; set; }
public Thumbnail thumbnail { get; set; }
public List<Image> images { get; set; }
public Creators creators { get; set; }
public Characters characters { get; set; }
public Stories stories { get; set; }
public Events events { get; set; }
}
public class Data
{
public int offset { get; set; }
public int limit { get; set; }
public int total { get; set; }
public int count { get; set; }
public List<Result> results { get; set; }
}
public class Rooto
{
public int code { get; set; }
public string status { get; set; }
public string copyright { get; set; }
public string attributionText { get; set; }
public string attributionHTML { get; set; }
public string etag { get; set; }
public Data data { get; set; }
}

}

主页

private async void Button_Clicked(object sender, EventArgs e)
{
var texto = caja.Text;
var request = new HttpRequestMessage();
request.RequestUri = new Uri("https://gateway.marvel.com/v1/public/comics?title=hulk&ts=1&apikey=###&hash=###");
request.Method = HttpMethod.Get;
request.Headers.Add("Accept", "application/json");

var client = new HttpClient();
HttpResponseMessage response = await client.SendAsync(request);
string content = await response.Content.ReadAsStringAsync();
Result dataObjects = JsonConvert.DeserializeObject<Result>(content);
JObject myJObject = JObject.Parse(content);
Console.WriteLine(myJObject);
List<Result> parsedFields = new List<Result>();
parsedFields.Add(dataObjects);

ListDemo.ItemsSource = parsedFields;

我的xaml和json

https://gateway.marvel.com/v1/public/comics?title=hulk&ts=1&apikey=ab07bf416406297274b27ca941ba3bee&hash=cf9eb501d7c3775c32b72c61a6a76805

<ContentPage.Content>
<StackLayout>
<Label Text="Control ListView"  FontSize="40" 
HorizontalOptions="Center" 
VerticalOptions="CenterAndExpand" />
<Button Text="Lamado API" Clicked="Button_Clicked"/>
<Entry x:Name="caja" Text=""/>
<ListView x:Name="ListDemo">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Image Source="{Binding resourceURI}" 
WidthRequest="50" 
HeightRequest="50"/>
<StackLayout Orientation="Vertical">
<Label Text="{Binding title}" 
FontSize="15" TextColor="Blue"/>
<Label Text="{Binding isbn}" 
FontSize="12" TextColor="Fuchsia"/>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>

tnaka

您可以尝试将它绑定到一个可观测集合。

private ObservableCollection<Result> _parsedFields = new ObservableCollection<Result>();
public ObservableCollection<Result> ParsedFields {
get{ return _parsedFields;}
set{ 
_parsedFields = value ;
OnPropertyChanged("ParsedFields"); 
}
}

确保实现INotifyPropertyChanged

public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}

然后在你的按钮功能:

var dataObjects = JsonConvert.DeserializeObject<ObservableCollection<Result>>(content);
ParsedFields = dataObjects;
ListDemo.ItemsSource = ParsedFields;

根级别应该是Root而不是Result类。

修改您的代码如下

Root dataObjects = JsonConvert.DeserializeObject<Root>(text);         
ListDemo.ItemsSource = dataObjects.data.results;

最新更新