I’m working on silverlight rss reader as schoolproject and I have one problem.
I want to define list of feed sources in xml and load this xml to list of button, each button for one feed.
xml looks like
<FeedList>
<Feed ButtonContent="HDRip's on RlsLog.net" Url="http://www.rlslog.net/category/movies/hdrip/feed/" />
</FeedList>
I’m loading this xml using linq and creating button
XDocument xdoc = XDocument.Load(string.Format("feeds.xml"));
if (xdoc != null)
{
var feedlist =
(from l in xdoc.Descendants("Feed")
select new MyButtons
{
Content = l.Attribute("ButtonContent").Value,
FeedUrl = l.Attribute("Url").Value
}
).ToList();
foreach (MyButtons feedbutton in feedlist)
{
Button b1 = new Button();
b1.Content = feedbutton.Content;
b1.Click += (s, e) => { feedViewer.LoadFeed(feedbutton.FeedUrl); };
ButtonPanel.Children.Add(b1);
}
}
button content is loading fine but that feed url is used the last one in xml for all buttons. could you please advice me what i’m doing wrong?
It looks like
feedbuttonis being captured as an iteration variable, thus causing the behavior you’re describing when you assign the event handler with the lambda expression.Try this instead:
Eric Lippert has blogged about this topic: