lundi 29 juin 2015

Windows 10 Universal App - ListView Update from Background Thread

I have a strange problem here. We develop a Windows 10 Universal App and now I want to update my listview when I add new value. But unfortunately it wont work and I dont really know why. When I add new value it won't update my list view.

The data comes from a background-thread (REST-Request against Server) and therefore I know, I should use something that runs the "add-functionality" on the UI-Thread.

First of all I declared a IProgress and my collection:

private List<dtoGemeinde> _listeGemeinden = new List<dtoGemeinde>();


public List<dtoGemeinde> GemeindenCollection
{
            get { return this._listeGemeinden; }
}

IProgress<dtoGemeinde> prog;

prog = new Progress<dtoGemeinde>(UpdateListViewUI);

This is the "UpdateListViewUI" method:

 public void UpdateListViewUI(dtoGemeinde dto)
 {
           _listeGemeinden.Add(dto);

            this.listViewGemeinden.ItemsSource = GemeindenCollection;
 }

And this is the callback method which is called when the background thread, which loads the data from the server, is finished:

 public async void onCallBackGemeinden(List<dtoGemeinde> listeGemeinden)
        {
            if (listeGemeinden != null && listeGemeinden.Count > 0)
            {
                this.progress.Visibility = Windows.UI.Xaml.Visibility.Collapsed;

                foreach (dtoGemeinde dto in listeGemeinden)
                {
                    await listViewGemeinden.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () => prog.Report(dto));              
                }                          
            }
            else
            {
                await new MessageDialog("Data cant be load", "Error").ShowAsync();
            }
        }

Aucun commentaire:

Enregistrer un commentaire