c# read a list of anonymous type occurs an error with a foreach

与世无争的帅哥 提交于 2019-12-08 12:50:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!