I am building a rss feed reader.I created a starter app using Visual studio.On it's main page I added a link to a new pivot page.All the rss thing happens in my pivot page.Now in my rss feed listbox,I initially set some list items using the following code:
public PivotPage1()
{
InitializeComponent();
getMeTheNews();
addToCollection("Android is going up","TechCrunch");
lstBox.DataContext = theCollection;
}
private void addToCollection(string p1, string p2)
{
theCollection.Add(new NewsArticle(p1,p2));
}
Here are the other two functions where rss is fetched from the server and parsed,But when I want to add processed entries to the ObservableCollection
in getTheResponse()
method,it results in the invalid cross thread access error.Any ideas?
Code:
private void getMeTheNews()
{
String url = "http://rss.cnn.com/rss/edition.rss";
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
webRequest.BeginGetResponse(getTheResponse, webRequest);
}
private void getTheResponse(IAsyncResult result)
{
HttpWebRequest request = result.AsyncState as HttpWebRequest;
if (request != null)
{
try
{
WebResponse response = request.EndGetResponse(result);
XDocument doc = XDocument.Load(response.GetResponseStream());
IEnumerable<XElement> articles = doc.Descendants("item");
foreach (var article in articles) {
System.Diagnostics.Debug.WriteLine(article);
try
{
System.Diagnostics.Debug.WriteLine(article.Element("title").Value);
addToCollection(article.Element("title").Value,"CNN");
}
catch (NullReferenceException e) {
System.Diagnostics.Debug.WriteLine(e.StackTrace);
}
}
}
catch(WebException e) {
System.Diagnostics.Debug.WriteLine(e.StackTrace.ToString());
}
}
else
{
}
}