Solved

Updating list view with repeated API calls

Posted on 2016-08-05
4
58 Views
Last Modified: 2016-08-09
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
Comment
Question by:Soumen Roy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 9

Expert Comment

by:Paweł
ID: 41747368
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
 
LVL 9

Expert Comment

by:Paweł
ID: 41747431
If that doesn't work, could you post your APIDataManager.PopulateAPIDataAsync method
0
 
LVL 9

Accepted Solution

by:
Paweł earned 500 total points
ID: 41747486
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
 

Author Closing Comment

by:Soumen Roy
ID: 41748661
Thanks for your help. It worked nicely :-)
0

Featured Post

Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…
Suggested Courses

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question