Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

ListView ItemsSource problems

$
0
0

Hi,

I'm trying to create simple App to show data from web service in ListView.
When I'm trying to set ListView.ItemsSource from async event handler - nothing happens until device is rotated. If I remove "await" from event handler - items populating immediately.
Here is simplified code sample (GetData() just returns dummy numbers):

public partial class Main {
    public Main() {
        InitializeComponent();
        Appearing += HandleAppearing;
    }
    async Task<int[]> GetData() {
        return await Task.Run<int[]>(() => { return new int[0]; });
    }
    async void HandleAppearing(object sender, EventArgs e) {
        //tickets -> ListView control
        //dummy call - for demo
        var data = await GetData();
        var list = data.ToList();
        List<MyData> d = new List<MyData>();
        d.Add(new MyData() { ID = "1", Subject = "2" });
        d.Add(new MyData() { ID = "1", Subject = "2" });
        //Option #1
        Xamarin.Forms.Device.BeginInvokeOnMainThread(() => { tickets.ItemsSource = d; });
        //Option #2
        //tickets.ItemsSource = d; 
    }
}
public class MyData {
    public string ID { get; set; }
    public string Subject { get; set; }
}

If I comment out "var data = await GetData()" - it starts working.
(Main - is ContentPage, ListView is defined in .xaml )


Viewing all articles
Browse latest Browse all 58056

Trending Articles