Xamarin - Picker View with list

3

I'm creating an application with Xamarin Forms and I came across a problem. In one of my Views I need to have an element filled with a list coming from a web service. In the case, in my ViewModel I was able to connect to the web service and fill a list with the objects I got from there, but I do not know how to play this on the Picker. My classes look like this:

View:

<ContentPage.BindingContext>
    <vm:BuscaCidadeDestinoViewModel></vm:BuscaCidadeDestinoViewModel>
</ContentPage.BindingContext>
 <StackLayout VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
        <!--Combobox-->
        <Label FontAttributes="Bold" FontSize="Medium">Selecione a Origem:</Label>
        <Picker  ItemsSource="{Binding ?????}"/>
    </StackLayout>

Class in the Model folder:

public class CidadeDestino
{
    public string DsCidadeDestino { get; set; }

}

And the ViewModel:

public ObservableCollection<CidadeDestino> ListaCidades { get; set; }
public BuscaCidadeDestinoViewModel()
    {

        this.ListaCidades = new ObservableCollection<CidadeDestino>();          
    }

public async Task GetCidades()
    {
        Aguarde = true;
        //abre uma conexão do tipo Http
        HttpClient cliente = new HttpClient();
        //retorna uma string "GetStringAsync" e é armazenada na variavel resultado
        var resultado = await cliente.GetStringAsync(urlListaCidades);
        //"Divide" a string recebida em diversos objetos e armazena na variavel cidadeJson
        var cidadeJson = JsonConvert.DeserializeObject<CidadeJson[]>(resultado);

        foreach (var cidades in cidadeJson)
        {
            this.ListaCidades.Add(new CidadeDestino
            {
                DsCidadeDestino = cidades.CidadeDestino

            });
        }
    }

I have already checked and the "CitiesList" is correctly populated with the values contained in the web service, so that if I change the "Picker" by a "ListView" in my View the values appear without problems. Thank you in advance for anyone who can help.

Sincerely.

    
asked by anonymous 13.06.2018 / 21:13

1 answer

1

For the good of all and general happiness of nations, from the version 2.3.4-pre of the Xamarin.Forms control Picker has now its main bindables properties. This operation was much more boring earlier.

Thanks to this improvement, we can bind with the list of options and with the selected item.

Using this other feature, you can add a property in your View Model to keep the item selected (and I also do not think it's a good idea to leave the property holding the list with set public, this can give a headache).

So, for practical purposes, your View Model would look like this:

public CidadeDestino CidadeSelecionada { get; set; }

public ObservableCollection<CidadeDestino> ListaCidades { get; }
public BuscaCidadeDestinoViewModel()
{
    ListaCidades = new ObservableCollection<CidadeDestino>();          
}

... // O resto do seu código

And on your screen you can do this:

<ContentPage.BindingContext>
    <vm:BuscaCidadeDestinoViewModel/>
</ContentPage.BindingContext>
<StackLayout VerticalOptions="CenterAndExpand" 
             HorizontalOptions="CenterAndExpand">
    <!--Combobox-->
    <Label FontAttributes="Bold" 
           FontSize="Medium"
           Text="Selecione a Origem"/>
    <Picker ItemsSource="{Binding ListaCidades}"
            SelectedItem="{Binding CidadeSelecionada}"/>
</StackLayout>

I hope I have helped.

    
15.06.2018 / 03:21