1

I have the following code:

    <ContentPage.BindingContext>
        <local:PisterosViewModel/>
    </ContentPage.BindingContext>

<Picker x:Name="pck_Pisteros"
                        ItemDisplayBinding="{Binding PisteroN}"
                        ItemsSource="{Binding PisterosLista}"
                        SelectedItem="{Binding PisterosLista}"
                        Title="Seleccione el usuario..."/>

Then my Model:

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

namespace ServLottery.Models
{
    public class Pisteros
    {
        public string PisteroID { get; set; }
        public string PisteroN { get; set; }

    }

}

and the view model:

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

namespace ServLottery.Models
{
    public class PisterosViewModel
    {
        public IList<Pisteros> PisterosLista { get; set; }

        public PisterosViewModel()
        {
            try
            {
                PisterosLista = new ObservableCollection<Pisteros>();
                GetPisteros();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private async void GetPisteros()
        {
            try
            {
                RestClient client = new RestClient();
                var pist = await client.Get<Models.Pisteros>("https://servicentroapi.azurewebsites.net/api/Pisteros");
                if (pist != null)
                {
                    PisterosLista = pist;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

    }
}

I set an breakpoint in var pist and it does have the values, then Pisteros list seems to get the values too, and this is executed when the page load, so I don't understand what's the problem, but the picker never shows the options.

Breakpoint 1 Breakpoint 2

1 Answers1

2

Welcome to SO !

It seems like BindingContext in Xaml can not deal with the dynamical data ,such API data from web server .

There is a workaround binding ItemSource dynamically by using coding in ContentPage , also can refer to this officail sample .

Therefore , adding code in Page.Xaml.cs as follow :

protected override async void OnAppearing()
{
    pck_Pisteros.ItemsSource = await GetTasksAsync();
    base.OnAppearing();
}

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

    return PisterosLista;
}

Now it will show:

enter image description here

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