xamarin/Xamarin.Forms

[Bug][UWP] SelectedItem of ListView on UWP always returns null when populated with webapi data

Ouverte

#7 954 ouverte le 11 oct. 2019

 (2 commentaires) (0 réaction) (0 personne assignée)C# (1 926 forks)batch import
a/listviewe/4 :clock4:help wantedinactivep/UWPt/bug :bug:up-for-grabs

Métriques du dépôt

Stars
 (5 644 étoiles)
Métriques de merge PR
 (Aucune PR mergée en 30 j)

Description

Description

This issue only happens on UWP. No problem on Android and I have not tested it on iOS yet.

  • ListView on MainPage is populated with web api data.
  • When we click any item, we should be navigated to its edit page. However because SelectedItem always returns null (only on UWP) then we never reach the edit page.
  • No problem if I populate the list view with hard-coded data.

The following code snippet is the focal point of the issue.

protected override async void OnAppearing()
{
    base.OnAppearing();

    // If I populate listview with data from web api
    // then e.SelectedItem becomes always null.
    // This only happens on UWP. 
    // It works just fine on Android. I haven't tested on iOS.
    listViewTodo.ItemsSource = await ts.GetTodoItemsAsync();
            
            

    // But if I populate listview with hard-coded data, 
    // there is no problem on UWP (and Android).
    //listViewTodo.ItemsSource = new TodoItem[] 
    //{
    //    new TodoItem { Text = "Accompanying ...", Complete = true },
    //    new TodoItem { Text = "Bathing ...", Complete = false }
    //};
}


private async void ListViewTodo_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
    // ti becomes always null on UWP if listview is populated with web api data
    TodoItem ti = e.SelectedItem as TodoItem;

    if (ti != null)
        await Navigation.PushAsync(new UpdatePage(ti));
}

Steps to Reproduce

See my repo: https://github.com/samrgc/Bug_UWP_Original. There are two solutions:

  • One for backend web api
  • One for Xamarin.Form clients

Note: Don't forget to adjust

public TodoService()
{
    msc = new MobileServiceClient("http://192.168.1.2:45455");
    mst = msc.GetTable<TodoItem>();
}

Guide contributeur