c#读取匿名类型列表与foreach发生错误
我需要从这个列表中获取数据,但是当我把我的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(对象发送者,HtmlAgilityPack.HtmlDocumentLoadCompleted e)第58行+ 0x6字节C#
上述就是C#学习教程:c#读取匿名类型列表与foreach发生错误分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/985325.html