Link to home
Start Free TrialLog in
Avatar of Soumen Roy
Soumen RoyFlag for India

asked on

Updating list view with repeated API calls

Hi Experts,

I am developing a Universal Windows Platform software with VS2015 CE. I am calling some web API through JSON and passing those data to my application.

I have one list view that displays a list of data call from various APIs. The whole list view needs to be replaced with a button click event. The list is being generated the first time when the application is loading. However the list does not seem to be refreshed with the desired button click.

Part of my Code is as follows:
-------------------------------------------

private void btnCur_Click(object sender, RoutedEventArgs e)
        {
            if (btnCur.Content.Equals("USD"))
                btnCur.Content="EUR";

            else
                btnCur.Content = "USD";

            gdContents(Convert.ToString(btnCur.Content));
        }

private async void gdContents(string un)
        {
            MyProgressRing.IsActive = true;
            MyProgressRing.Visibility = Visibility.Visible;

            //The APIData class has a function to manage the API calls and put them in a list
            Data = new ObservableCollection<APIData>();     
            await APIDataManager.PopulateAPIDataAsync(Data, un);

            MyProgressRing.IsActive = false;
            MyProgressRing.Visibility = Visibility.Collapsed;
        }

Open in new window


Regards,
Soumen
Avatar of Paweł
Paweł
Flag of Switzerland image

your issue is most likely a threading one, by making your gdContents async but returning void your application isn't syncing with the UI thread (that maybe a bad explanation)

to fix it, set your button click as async
make your gdContents methode return a Task then at the end of your method return a null object.

 return Task.FromResult<object>(null);

private async void btnCur_Click(object sender, RoutedEventArgs e)
        {
            if (btnCur.Content.Equals("USD"))
                btnCur.Content="EUR";

            else
                btnCur.Content = "USD";

          await gdContentsAsync(Convert.ToString(btnCur.Content));
        }

private async Task<object> gdContentsAsync(string un)
        {
            MyProgressRing.IsActive = true;
            MyProgressRing.Visibility = Visibility.Visible;

            //The APIData class has a function to manage the API calls and put them in a list
            Data = new ObservableCollection<APIData>();     
            await APIDataManager.PopulateAPIDataAsync(Data, un);

            MyProgressRing.IsActive = false;
            MyProgressRing.Visibility = Visibility.Collapsed;

            return await Task.FromResult<object>(null);
        }

Open in new window


try that and let me know if it works

Ok try that
If that doesn't work, could you post your APIDataManager.PopulateAPIDataAsync method
ASKER CERTIFIED SOLUTION
Avatar of Paweł
Paweł
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Soumen Roy

ASKER

Thanks for your help. It worked nicely :-)