digital_ice7 0 Light Poster

how do i make my windows phone app to wait for webclient to finish before continuing?

TextBlock.Text = originalFeed.Count.ToString();

this part of the program should returned the total rss feed downloaded from the rss links but it is returning 0 instead eventho there are rss feeds.

using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;
using SQLite;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceModel.Syndication;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Xml;
using Windows.ApplicationModel;
using Windows.Storage;

namespace RssNewsFeed
{
    public partial class FeedPage : PhoneApplicationPage
    {
        List<ITEMS> originalFeed = new List<ITEMS>();
        List<string> listOFfeed = new List<string>();

        int source = 0;

        public FeedPage()
        {
            InitializeComponent();

            listOFfeed.Add("http://www.thestar.com.my/RSS/Editors-Choice/Nation.aspx");
            listOFfeed.Add("http://www.thestar.com.my/RSS/Editors-Choice/Tech.aspx");

            for (int i = 0; i < listOFfeed.Count; i++)
            {
                WebClient webClient = new WebClient();
                webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
                webClient.DownloadStringAsync(new System.Uri(listOFfeed[i]));
            }

            TextBlock.Text = originalFeed.Count.ToString(); // this is returning 0
        }

        private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show(e.Error.Message);
                });
            }
            else
            {
                this.State["feed"] = e.Result;

                UpdateFeedList(e.Result);
            }
        }

        private void UpdateFeedList(string feedXML)
        {
            StringReader stringReader = new StringReader(feedXML);
            XmlReader xmlReader = XmlReader.Create(stringReader);
            SyndicationFeed feed = SyndicationFeed.Load(xmlReader);

            foreach (var item in feed.Items)
            {
                originalFeed.Add(new ITEMS { Title = item.Title.Text, Summary = item.Summary.Text, Link = item.Links[0].Uri.OriginalString, Source = source });
            }

            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                feedListBox.ItemsSource = feed.Items;
            });
        }

        private void feedListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox listBox = sender as ListBox;

            if (listBox != null && listBox.SelectedItem != null)
            {
                SyndicationItem sItem = (SyndicationItem)listBox.SelectedItem;

                if (sItem.Links.Count > 0)
                {
                    Uri uri = sItem.Links.FirstOrDefault().Uri;

                    WebBrowserTask webBrowserTask = new WebBrowserTask();
                    webBrowserTask.Uri = uri;
                    webBrowserTask.Show();
                }
            }
        }

        private class ITEMS
        {
            public string Title { get; set; }
            public string Summary { get; set; }
            public string Link { get; set; }
            public int Source { get; set; }
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.