0

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.

1 Answers1

1

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 :

enter image description here

Junior Jiang
  • 12,430
  • 1
  • 10
  • 30