Search code examples
c#xamlwindows-phone-8

LongListSelector scrolling issue after adding items to it


I am showing YouTube videos in a LongListSelector. New videos are added (20 at a time) when the person reaches the end of the list for which I am using a pull to refresh class from Here.

Now new videos are showing in the list, but I am only able to scroll through 20 of them. If i scroll beyond that, I can see the list items, but it scrolls back with a rubber band effect. Now when I tap on any item that are visible, I am sent to the next page, and when I hit the back button, the LongListSelector is now showing all the items with no problem.

Here is the code:

private void GetYoutubePlaylist(string feedXML)
{
    try
    {
        StringReader stringReader = new StringReader(feedXML);
        XmlReader xmlReader = XmlReader.Create(stringReader);
        SyndicationFeed feed = SyndicationFeed.Load(xmlReader);


        YoutubeVideo video = null;

        foreach (SyndicationItem item in feed.Items)
        {
            video = new YoutubeVideo();

            video.YoutubeLink = item.Links[0].Uri;
            string a = video.YoutubeLink.ToString().Remove(0, 31);
            video.Id = a.Substring(0, 11);
            video.Title = item.Title.Text;
            video.PubDate = item.PublishDate.DateTime;

            video.Thumbnail = YouTube.GetThumbnailUri(video.Id, YouTubeThumbnailSize.Small);

            videosList.Add(video);
        }
        if (video != null)
        {
            MainListBox.ItemsSource = videosList;

        }
    }
    catch { }
}

How can I fix this issue?


Solution

  • After a lot of head scratching i am able to do this. All i had to was, before assigning videoslist as the ItemSource for MainListBox, i assigned null to ItemSource. Working great

    here is the final code.

    private void GetYoutubePlaylist(string feedXML)
            {
             var last = new YoutubeVideo(); //scroll to this item when new items are loaded
            if(videosList.Count > max_results)
                last = videosList[videosList.Count - 11];
            try
            {
                StringReader stringReader = new StringReader(feedXML);
                XmlReader xmlReader = XmlReader.Create(stringReader);
                SyndicationFeed feed = SyndicationFeed.Load(xmlReader);
    
                YoutubeVideo video;
    
                foreach (SyndicationItem item in feed.Items)
                {
                    video = new YoutubeVideo();
    
                    video.YoutubeLink = item.Links[0].Uri;
                    string a = video.YoutubeLink.ToString().Remove(0, 31);
                    video.Id = a.Substring(0, 11);
                    video.Title = item.Title.Text;
                    video.PubDate = item.PublishDate.DateTime;
    
                    video.Thumbnail = YouTube.GetThumbnailUri(video.Id, YouTubeThumbnailSize.Small);
    
                    videosList.Add(video);
                }
    
                MainListBox.ItemsSource = null;
                MainListBox.ItemsSource = videosList;
                if(last.Id!=null)
                MainListBox.ScrollTo(last as YoutubeVideo);
            }
            catch {            }
        }