Xamarin API Json 列表到 ListView [英] Xamarin API Json List to ListView

查看:25
本文介绍了Xamarin API Json 列表到 ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码这是我的属性:

使用系统;使用 System.Collections.Generic;使用 System.Text;命名空间 ServLottery.Models{公共课 PendientesGiro{公共 int IDPendGiros { 获取;放;}公共字符串 NumFactura { 获取;放;}公共双蒙托{得到;放;}公共 int IDPisteroVenta { 获取;放;}公共 int IDPisteroCanje { 获取;放;}公共字符串标识{获取;放;}公共字符串 Nombre { get;放;}公共字符串 FechaVenta { 获取;放;}}}

然后是视图模型:

使用系统;使用 System.Collections.Generic;使用 System.Collections.ObjectModel;使用 System.Text;命名空间 ServLottery.Models{公共类 PendGirosViewModel{公共 IList<PendientesGiro>PendientesLista { 得到;放;}公共 PendGirosViewModel(){尝试{PendientesLista = new ObservableCollection();GetPendGiros();}捕获(异常前){扔前;}}私有异步 void GetPendGiros(){尝试{RestClient 客户端 = new RestClient();var pend = await client.Get(https://servicentroapi.azurewebsites.net/api/Giros");如果(挂起!= null){PendientesLista = 挂起;}}捕获(异常前){扔前;}}}}

以及我用来从 API 取回数据的函数

private async Task>GetPendientesAsync(){列表PendientesLista = new List();HttpClient 客户端 = 新的 HttpClient();Uri uri = new Uri(string.Format("https://servicentroapi.azurewebsites.net/api/Giros", string.Empty));HttpResponseMessage response = await client.GetAsync(uri);如果(响应.IsSuccessStatusCode){字符串内容 = 等待 response.Content.ReadAsStringAsync();PendientesLista = JsonConvert.DeserializeObject>(content);Console.WriteLine("内容::"+内容);Console.WriteLine("数据::" + PendientesLista);}返回 PendientesLista;}

lv_Pend.ItemsSource = await GetPendientesAsync();

信息到达 ItemsSource 但未显示在 ListView 中,我认为我的问题在于 xaml 代码

<ListView.ItemTemplate><数据模板><StackLayout Orientation=水平"><标签文本="{绑定标识}";TextColor=白色"/><Label Text="{绑定名称}";TextColor=白色"/></StackLayout></数据模板></ListView.ItemTemplate></ListView>

我希望我的列表显示身份和名称,并且当我选择时能够获取与另一个页面相关的整个日期集.

解决方案

欢迎来到 SO!

从共享的 Xaml 代码中,DataTemplate 中缺少一个 ViewCell 属性.检查一下

I have the following code Here's my properties:

using System;
using System.Collections.Generic;
using System.Text;

namespace ServLottery.Models
{
    public class PendientesGiro
    {
        public int IDPendGiros { get; set; }
        public string NumFactura { get; set; }
        public double Monto { get; set; }
        public int IDPisteroVenta { get; set; }
        public int IDPisteroCanje { get; set; }
        public string Identification { get; set; }
        public string Nombre { get; set; }
        public string FechaVenta { get; set; }
    }
}

Then the view model:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace ServLottery.Models
{
    public class PendGirosViewModel
    {
        public IList<PendientesGiro> PendientesLista { get; set; }

        public PendGirosViewModel()
        {
            try
            {
                PendientesLista = new ObservableCollection<PendientesGiro>();
                GetPendGiros();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private async void GetPendGiros()
        {
            try
            {
                RestClient client = new RestClient();
                var pend = await client.Get<Models.PendientesGiro>("https://servicentroapi.azurewebsites.net/api/Giros");
                if (pend != null)
                {
                    PendientesLista = pend;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
    }
}

and the function I'm using to get the data back from the API

private async Task<List<PendientesGiro>> GetPendientesAsync()
        {
            List<PendientesGiro> PendientesLista = new List<PendientesGiro>();
            HttpClient client = new HttpClient();
            Uri uri = new Uri(string.Format("https://servicentroapi.azurewebsites.net/api/Giros", string.Empty));
            HttpResponseMessage response = await client.GetAsync(uri);
            if (response.IsSuccessStatusCode)
            {
                string content = await response.Content.ReadAsStringAsync();
                PendientesLista = JsonConvert.DeserializeObject<List<PendientesGiro>>(content);
                Console.WriteLine("content :: " + content);
                Console.WriteLine("Data :: " + PendientesLista);
            }
            return PendientesLista;
        }

lv_Pend.ItemsSource = await GetPendientesAsync();

Info arrives to ItemsSource but is not being shown in the ListView, I think my problem is with the xaml code

<ListView x:Name="lv_Pend"
                      SelectedItem="{Binding PendientesGiro}"
                      ItemsSource="{Binding PendientesGiro}">
                
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackLayout Orientation="Horizontal">
                            <Label Text="{Binding Identification}" TextColor="White"/>
                            <Label Text="{Binding Name}" TextColor="White"/>
                        </StackLayout>
                    </DataTemplate>
                </ListView.ItemTemplate>
                
            </ListView>

I want my list to show the identification and name and when I selected be able to take the entire set of date related to another page.

解决方案

Welcome to SO !

From shared Xaml code , there missing a ViewCell property inside DataTemplate. Have a check with Custom Cells of ListView , therfore you can modify as follow :

<ListView x:Name="lv_Pend"
            SelectedItem="{Binding PendientesGiro}"
            ItemsSource="{Binding PendientesGiro}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell> // add ViewCell
                <StackLayout Orientation="Horizontal">
                    <Label Text="{Binding Identification}"
                            TextColor="Black" />
                    <Label Text="{Binding Name}"
                            TextColor="Black" />
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

It will work :

这篇关于Xamarin API Json 列表到 ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆