我需要从这个列表中获取数据,但是当我把我的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)第58行+ 0x6字节C#