【问题标题】:Bind Data from JSON to ListView (Xamarin.Forms Cross Platform C#)将数据从 JSON 绑定到 ListView(Xamarin.Forms 跨平台 C#)
【发布时间】:2017-08-02 09:01:28
【问题描述】:

目前我被困在如何将 JSON 数据传递给列表视图。

背景信息:软件会定期创建一个 json 文件,该文件可通过网络服务获得(示例如下)。此数据应解析为用户可以查看进程的列表视图。

JSON 如下例所示(最多可包含 30 个进程)

{
  "file_timestamp”: "00:00:00 AM",
  "process": [{
      "id": "1",
      "fzg_nr": "0123",
      "fzg_kz": "KHSKDNJK",
      "timestamp": "03:53:25 AM"
    },
    {
      "id": "2",
      "fzg_nr": "0124",
      "fzg_kz": "UFJSDK",
      "timestamp": "04:53:25 AM"
    },
    {
      "id": "3",
      "fzg_nr": "0125",
      "fzg_kz": "KDFJKLD",
      "timestamp": "05:53:25 AM"
    }
  ]
}

这是我的 C# 代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace dfg
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class JsonParsingPage : ContentPage
    {
        public JsonParsingPage()
        {
            InitializeComponent();
            GetJSON();
        }

        public async void GetJSON()
        {
            try
            {
                var client = new System.Net.Http.HttpClient();
                var response = await client.GetAsync("http://myinternaliis/xample.JSON");
                string json = await response.Content.ReadAsStringAsync();
                RootObject rootObject = new RootObject();
                ListView listViewJson = new ListView();
                if (json != "")
                {
                    rootObject = JsonConvert.DeserializeObject<RootObject>(json);
                }
                listViewJson.ItemsSource = rootObject.processes;
                Content = listViewJson;
            }
            catch (InvalidCastException e)
            {
                throw e;
            }
            ProgressLoader.IsVisible = false;
        }

        public class Process
        {
            public string id { get; set; }
            public string fzg_nr { get; set; }
            public string fzg_kz { get; set; }
            public string timestamp { get; set; }
        }

        public class RootObject
        {
           public string file_timestamp { get; set; }
           public List<Process> processes { get; set; }
        }
    }
}

XAML 文件只包含进度加载器。

非常感谢任何帮助、提示或参考。提前谢谢,保罗

编辑:我的问题是,列表视图中没有显示数据...

http://imgur.com/a/c47vW

【问题讨论】:

  • 1-邮政编码,不是图片。 2-你的问题是什么?我在图像上看到了您如何将数据传递给 ListView。
  • 首先:你试过什么?我们无法从您提供的内容中真正看出。确切的问题是什么? 不要插入代码作为屏幕截图。将其粘贴为格式化代码。请参阅How to Askminimal reproducible example,了解如何提出好的问题。
  • 真的很抱歉伙计们,我很难将代码格式化到这个线程中。现在应该没问题了。
  • 您的 json 样本有一个名为“process”的属性,而您的 rootobject 类有一个名为“processes”的属性

标签: c# json listview xamarin


【解决方案1】:

我认为问题在于您的ListView 没有定义ItemTemplate,所以它不知道必须显示哪些信息。

链接的屏幕截图正好说明了这一点 - 您可以查看 ListView 中的“行”,但您无法查看此行的任何数据。

您可以使用基本的DataTemplate 来显示已解析的 JSON 对象的一些属性,如下所示:

DataTemplate template = new DataTemplate(typeof(TextCell));
template.SetBinding(TextCell.TextProperty, "id");
template.SetBinding(TextCell.DetailProperty, "fzg_kz");

listViewJson.ItemTemplate = template;

【讨论】:

  • 我更改了代码,但不幸的是它没有改变任何东西。所以我的json一定有问题你不觉得吗?我将更具体地区分错误。
【解决方案2】:

感谢到目前为止的回复。感谢你们,我可以使用这个问题!

这就是问题所在:

首先@Mario C Simao Jr 是对的。我必须将数据绑定到单元格,我按照他的建议做了。方法改成这样:

public async void GetJSON()
        {
            try
            {
                var client = new System.Net.Http.HttpClient();
                var response = await client.GetAsync("http://x.x.x.x/xample.JSON");
                string json = await response.Content.ReadAsStringAsync();
                RootObject rootObject = new RootObject();
                ListView listViewJson = new ListView();
                if (json != "")
                {
                    rootObject = JsonConvert.DeserializeObject<RootObject>(json);
                }
                DataTemplate template = new DataTemplate(typeof(TextCell));
                template.SetBinding(TextCell.TextProperty, "id");
                template.SetBinding(TextCell.DetailProperty, "fzg_kz");
                listViewJson.ItemTemplate = template;
                listViewJson.ItemsSource = rootObject.process;
                Content = listViewJson;
            }
            catch (InvalidCastException e)
            {
                throw e;
            }
            ProgressLoader.IsVisible = false;
        }

但所有问题的根源是public class RootObject。 我不得不把它改成这样:

public class RootObject
        {
           public string file_timestamp { get; set; }
           public List<Process> process { get; set; }
        }

所以这是我自己的经典第 8 层错误。

非常感谢您的帮助!

真诚的,保罗

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    • 2016-11-19
    • 1970-01-01
    • 2022-01-11
    相关资源
    最近更新 更多