Monday, December 31, 2012

OutOfMemoryException when trying to add items to ItemsControl when load in ItemsControl more items

OutOfMemoryException when trying to add items to ItemsControl when load in ItemsControl more items

I have a ItemsControl with ScrollViewer it shows newfeed. Answer why i do not use ListBox. I show 40-50 news, and then, I push the button, which updates newsfeed collection, and then - put it in ItemsControl. But I have OutOfMemoryException, and I can understand why. The method i use to get more newsfeed is the same method which loads data firsttime, so i think problem is not in it. But here is the code

        void c_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)     {         XDocument xml = XDocument.Load(e.Result);          App.WriteToIsoStorage(xml.ToString(), "items.xml");         App.New_From = (string)xml.Element("response").Element("new_from").Value;         var inf = from c in xml.Descendants("item")                    select c;         foreach (var item in inf)         {             if ((string)item.Element("type").Value != "friend" && item.Element("type").Value != "photo_tag" && (string)item.Element("type").Value != "photo" && item.Element("type").Value != "wall_photo")             {                 New new1 = new New();                 GetIds(item, new1, out new1);                 GetIcons(item, new1, out new1);                 if (item.Element("attachment") != null)                 {                     var vd = from c in item.Descendants("attachment")                                 select c;                     foreach (var content in vd)                     {                         if (content.Element("audio") != null) GetAudios(content, new1.audioAttachments, out new1.audioAttachments);                         if (content.Element("photo") != null && (string)item.Element("type").Value != "photo" && item.Element("type").Value != "wall_photo") GetPhotos(content, new1.photoAttachments, out new1.photoAttachments);                         if (content.Element("video") != null) GetVideos(content, new1.videoAttachments, out new1.videoAttachments);                         if (content.Element("link") != null) GetUrls(content, new1, out new1);                     }                 }                 if (item.Element("type").Value != "photo" && item.Element("type").Value != "wall_photo" && item.Element("type").Value != "photo_tag")                 NewsList.Add(new1);                 this.ItemsControl.Items.Add(new1);                 new1 = null;             }              IEnumerable<XElement> profiles = from c in xml.Descendants("user") //                                                  select c;             IEnumerable<XElement> groups = from c in xml.Descendants("group") //                                                  select c;             GetNames(profiles, groups);           }     } 

and here what VS shows .

Answers & Comments...




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog