如何在gridview中显示从.json中提取的数据

本文关键字:json 提取 数据 显示 gridview | 更新日期: 2023-09-27 17:50:37

我需要一些相对简单的帮助,但我找不到任何解决方案:我使用JSON。. NET从这里拉数据:https://yts.to/api/,我正在寻找一种方法来放置每个电影在一个网格视图,有一个图片框显示海报,标题和年份下面,像这个应用程序的概念我做了:https://i.stack.imgur.com/a5YUk.jpg

除此之外,我已经完成了大约一半的应用程序,但我对c#和XAML相当陌生,所以如果我可能有代码,那将是伟大的:)

如何在gridview中显示从.json中提取的数据

我建议您使用listbox而不是GridView。您可以使用ItemTemplate样式您的listbox。下面是一个示例

<ListBox Name="lstView" ItemsSource="{Binding}">
   <ListBox.ItemTemplate>
      <DataTemplate>
          <StackPanel Orientation="Horizontal">
             <Image Source="{Binding ImagePath}"></Image>
             <TextBlock Text="{Binding Name}"></TextBlock>
        </StackPanel>
      </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

C#绑定:

public class Article
{
    public string Name { get; set; }
    public string ImagePath { get; set; }
}

 Article article1 = new Article() { Name = "name1", ImagePath = "path of image 1" };
 Article article2 = new Article() { Name = "name2", ImagePath = "path of image 2" };
 var articles = new List<Article>();
 articles.Add(article1);
 articles.Add(article2);
 lstView.DataContext = articles;

你需要在JSON到Object的转换部分工作。