问题
I need to get data from this list, however when I put my foreach of a runtime error saying that the list contains null objects. But if I remove the foreach and put a breakpoint on the line where listbox.itemsSource receives the list, I see that I have the list loaded with all items correctly.
var imgs = e.Document.DocumentNode.SelectNodes(@"//img[@src]")
.Select(img => new
{
Link = img.Attributes["src"].Value,
Title = img.Attributes["alt"].Value,
}).ToList();
listBoxPopular.ItemsSource = imgs;
foreach (var item in imgs)
{
listBoxPopular.Items.Add(new PopularVideos(item.Title, item.Link));
}
Class:
class PopularVideos
{
public PopularVideos() { }
public PopularVideos(string titulo, string url)
{
Titulo = titulo;
BitmapImage Img = new BitmapImage(new Uri(url));
}
public string Titulo { get; set; }
public Uri Url { get; set; }
}
The error is:
An unhandled exception of type 'System.NullReferenceException' occurred in AppUnno.dll
Stack Trace:
AppUnno.dll!AppUnno.MainPage.DownLoadCompleted(object sender, HtmlAgilityPack.HtmlDocumentLoadCompleted e) Line 58 + 0x6 bytes C#
来源:https://stackoverflow.com/questions/17456330/c-sharp-read-a-list-of-anonymous-type-occurs-an-error-with-a-foreach