Forum Xamarin.Forms

Using Webservice does not fill ListView or Picker

torontoron ATMember ✭✭
edited August 2017 in Xamarin.Forms

Hi Guys,

Hope you could explain, what I'm doing wrong. Working on this since 2 days and it feels like I'm getting depressive :).
My Webservice is doing fine. When I start the debugger I see, that my ObservableCollection Person get filled with data. But the ListView and Picker are still empty.

class HomeViewModel : INotifyPropertyChanged

        public ObservableCollection<Person> Persons { get; set; }

        public HomeViewModel()

        public  void SavePerson(string firstname, string lastname)

        private async void LoadPersons()
            var personServices = new PersonServices();
            Persons = new ObservableCollection<Person>(await personServices.GetPersons());


        public event PropertyChangedEventHandler PropertyChanged;


<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns=""

            <Label Text="Debt App Home" FontSize="Large" HorizontalOptions="Center"
                    FontAttributes="Bold" />
            <Entry Placeholder="Betrag" Keyboard="Numeric"></Entry>
                    <RowDefinition Height="Auto"></RowDefinition>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                    <ColumnDefinition Width="*"></ColumnDefinition>

                <Label Text="Ausgabe" x:Name="lblSwitch" Grid.Row="0" Grid.Column="0"></Label>
                <Switch Toggled="ActionToogleEA" Grid.Row="0" Grid.Column="1" ></Switch>
            <Picker x:Name="personPicker" Title="bitte Person auswählen" ItemsSource="{Binding Persons}" ItemDisplayBinding="{Binding Firstname}">
            <Button Text="speichern"></Button>
            <ListView x:Name="DebtListView" ItemsSource="{Binding Persons}">
                                <Label Text="{Binding Firstname}"></Label>
                                <Label Text="{Binding Lastname}"></Label>

Binding seems to be ok, cause when I change my LoadPersons() method to

  private async void LoadPersons()
                    Persons = new ObservableCollection<Person>();
            Persons.Add(new Person{ID=1, Firstname="first", Lastname="last"});


it works.

How can this be, even the Person List gets filled by using the Webservice? Deserialization works as well.
This is where I call the Rest Service.

public async Task<List> GetPersons()
List persons = new List();
persons.Add(new Person { ID = 1, Firstname = "Alf", Lastname = "Luri" });
persons.Add(new Person { ID = 1, Firstname = "Mia", Lastname = "Meyr" });
persons.Add(new Person { ID = 1, Firstname = "Sepp", Lastname = "Karlson" });
persons.Add(new Person { ID = 1, Firstname = "Sepp", Lastname = "Karlson" });

        string restUri = "";

         var uri = new Uri(restUri);

         var response = await client.GetAsync(uri);
         if (response.IsSuccessStatusCode)
             var content = await response.Content.ReadAsStringAsync();
             persons = JsonConvert.DeserializeObject<List<Person>>(content);
        return persons;

Best Answer


  • CostasAletrariCostasAletrari USMember ✭✭

    On your api call your return type is

    public async Task<List> GetPersons()

    shouldn't you be returning a List

    public async Task<List<Person>> GerPersons();
  • torontoron ATMember ✭✭
    edited August 2017

    Hi @ReemaGarg
    you were right, that did it.
    And it makes sense filling the ObservableCollection like this.

    Just to get a better understanding and not making the same mistake again.
    There is another thread with a similar problem. Why is that working for him?

    He is also returning a List and assign it to an ObservableCollection.

    I've also tried to return an ObservableCollection instead of a List. That didn't solved the problem either.

Sign In or Register to comment.