Xamarin API Json列表到ListView [英] Xamarin API Json List to ListView
问题描述
我有以下代码 这是我的属性:
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; }
}
}
然后创建视图模型:
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;
}
}
}
}
以及我用来从API取回数据的函数
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();
信息到达ItemsSource,但是没有显示在ListView中,我认为我的问题出在xaml代码上
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.
推荐答案
欢迎使用SO!
在共享的 Xaml 代码中,DataTemplate
中缺少ViewCell
属性.请检查
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>
它将起作用:
这篇关于Xamarin API Json列表到ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!