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()
{
LoadPersons();
}
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="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="debtMobileApp.view.HomePage"
xmlns:ViewModels="clr-namespace:debtMobileApp.ViewModel">
<ContentPage.BindingContext>
<ViewModels:HomeViewModel/>
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout>
<Label Text="Debt App Home" FontSize="Large" HorizontalOptions="Center"
FontAttributes="Bold" />
<Entry Placeholder="Betrag" Keyboard="Numeric"></Entry>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Text="Ausgabe" x:Name="lblSwitch" Grid.Row="0" Grid.Column="0"></Label>
<Switch Toggled="ActionToogleEA" Grid.Row="0" Grid.Column="1" ></Switch>
</Grid>
<Picker x:Name="personPicker" Title="bitte Person auswählen" ItemsSource="{Binding Persons}" ItemDisplayBinding="{Binding Firstname}">
</Picker>
<Button Text="speichern"></Button>
<ListView x:Name="DebtListView" ItemsSource="{Binding Persons}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding Firstname}"></Label>
<Label Text="{Binding Lastname}"></Label>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
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 = "http://debtapp.ideflix.at/api/Person"; 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; }