GTIN无法转换为系统字符串

时间:2019-02-20 06:39:38

标签: xamarin xamarin.forms

我从usda api中检索了一些食物,并希望在列表视图中列出它们,它可以很好地检索产品,也列出它们,但列表项为空,并且在控制台中显示:

[0:] Binding: Sunshine Cheez-It Crackers Original 3oz, UNPREPARED, GTIN: 00024100219131 can not be converted to type 'System.String'
[0:] Binding: Sunshine Cheez-It Crackers Hot & Spicy 7oz, UNPREPARED, GTIN: 00024100705849 can not be converted to type 'System.String

问题:listitems为空,我不知道我的字符串转换有什么问题。 相关提示:对于这个项目,我导入了newtonsoftJSON和system.http

xaml.cs:

public partial class MainPage : ContentPage
    {
        public int start = 0;
        public MainPage()
        {
            InitializeComponent();
            LoadFood(start);
        }
        private async void LoadFood(int start)
        {
            lvwFoods.ItemsSource = await VoedselManager.GetFoodListAsync(start, start + 10);
        }

        private async void btnLoad_Clicked(object sender, EventArgs e)
        {
            start += 10;
            lvwFoods.ItemsSource = await VoedselManager.GetFoodListAsync(start, start + 10);
        }
    }
    public class Voedsel
    {
        [JsonProperty(PropertyName = "id")]
        public string Id { get; set; }
        [JsonProperty(PropertyName = "name")]
        public string Name { get; set; }

        public override string ToString()
        {
            return Name;
        }

    }
    public class VoedselManager
    {
        public static async Task<List<Voedsel>> GetFoodListAsync(int offset, int number)
        {
            try
            {
                String apikey = "";
                String url = string.Format("https://api.nal.usda.gov/ndb/list?format=json&lt=f&sort=id&api_key={0}&offset={1}&max={2}", apikey, offset, number);

                HttpClient client = new HttpClient();
                client.DefaultRequestHeaders.Add("Accept", "application/json");

                String result = await client.GetStringAsync(url);
                JObject fullObject = JsonConvert.DeserializeObject<JObject>(result);
                JToken data = fullObject.SelectToken("list.item");

                return data.ToObject<List<Voedsel>>();
            }
            catch (Exception ex)
            { throw ex; }
        }

    }

xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:VoedselDemo"
             x:Class="VoedselDemo.MainPage">

    <StackLayout Margin="16">
        <Label Text="Choose a food" FontSize="Large" TextColor="Gray" />
        <!-- lijst van voedingsmiddelen (naam) -->
        <ListView x:Name="lvwFoods" ItemSelected="lvwFoods_ItemSelected" VerticalOptions="FillAndExpand" />
        <!-- knop die bij elke klik een aantal records extra toevoegt aan lvwFoods -->
        <Button x:Name="btnLoad" Clicked="btnLoad_Clicked" Text="load more..." />
    </StackLayout>
</ContentPage>

0 个答案:

没有答案