i’m currently developing an application to windows phone 7
i’m trying to get multiple rss feeds and display each one in a different ListBox
i created a custom class that is called
public class RssFeed
{
public string Title { get; set; }
public string Url { get; set; }
public ListBox MyListBox { get; set; }
}
i created a list of RssFeed and i’m trying to do the following
foreach (RssFeed item in items)
{
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler((sender, e) => this.webClient_DownloadStringCompleted(sender, e, item.MyListBox));
webClient.DownloadStringAsync(new System.Uri(item.Url));
}
i have the event
private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e,ListBox listBox)
the problem is that the parameter listBox is the same for all the events which is the last ListBox when i created event handler
for example: i have the List items which has
the first item has MyListBox which equals to ListBox1
the second item has MyListBox which equals to ListBox2
the event webClient_DownloadStringCompleted will be called always with the parameter ListBox2
what can i do to get different values for the parameter as it should. thank you
You are using a local variable “item” the wrong way.
Since the Lambda expression will be evaluated when the code actually runs (long after the foreach loop is over) the item variable will always point to the last element in the collection.
Solution: