Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 88
  • Last Modified:

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
0
Soumen Roy
Asked:
Soumen Roy
  • 3
1 Solution
 
PawełCommented:
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
0
 
PawełCommented:
If that doesn't work, could you post your APIDataManager.PopulateAPIDataAsync method
0
 
PawełCommented:
actually you're problem is in re-instantiating of your observable collection

Data = new ObservableCollection<APIData>();

by doing that your're essentially breaking your binding, try clearing it

if you do data.clear()

and then you get a thread marshaled error, then you have the problem that i mentioned before with an inability to communicate with your UI thread, you can hack it by using the

  await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
            () => { //  });


so if your xaml is

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <ListView ItemsSource="{Binding MyThings, Mode=TwoWay}"/>
        <Button Content="ToFrench" Click="Button_Click"/>
    </Grid>
</Page>

Open in new window


and you're code-behind is

using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace App1
{
    public sealed partial class MainPage : Page
    {
        public ObservableCollection<string> MyThings { get; set; }
        public MainPage()
        {
            MyThings = new ObservableCollection<string>();
            MyThings.Add("One");
            MyThings.Add("Two");
            MyThings.Add("Three");
            this.InitializeComponent();
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            await ToFrench();

        }

        async Task<object> ToFrench()
        {
            MyThings.Clear();
            await PopulateFrenchDataAsync(MyThings);
            return await Task.FromResult<object>(null);
        }

        async Task<object> PopulateFrenchDataAsync(ObservableCollection<string> Data)
        {
            await Task.Run(() =>
              
                {
                    Data.Add("un");
                    Data.Add("deux");
                    Data.Add("trois");

                });

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

Open in new window


change your update logic to

  async Task<object> PopulateFrenchDataAsync(ObservableCollection<string> Data)
        {
            await Task.Run(async () =>
                await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    Data.Add("un");
                    Data.Add("deux");
                    Data.Add("trois");

                }));

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

Open in new window

0
 
Soumen RoyAuthor Commented:
Thanks for your help. It worked nicely :-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now