我需要从此列表中获取数据,但是当我将我的 foreach 放入运行时错误时说该列表包含空对象。但是,如果我删除 foreach 并在 listbox.itemsSource 接收列表的行上放置一个断点,我会看到我已经正确加载了所有项目的列表。
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 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; }
}
错误是:
An unhandled exception of type 'System.NullReferenceException' occurred in AppUnno.dll
堆栈跟踪:
AppUnno.dll!AppUnno.MainPage.DownLoadCompleted(object sender, HtmlAgilityPack.HtmlDocumentLoadCompleted e) Line 58 + 0x6 bytes C#