Skip to content

Instantly share code, notes, and snippets.

@lawrencegripper
Last active January 12, 2016 23:54
Show Gist options
  • Save lawrencegripper/9396993 to your computer and use it in GitHub Desktop.
Save lawrencegripper/9396993 to your computer and use it in GitHub Desktop.
Setup infinite scrolling for windows store with gridview
<GridView Grid.Row="1" ItemsSource="{Binding Popular}" />
public class ScrollableObservableCollection : ObservableCollection<ApiSong>, ISupportIncrementalLoading
{
public ScrollableObservableCollection(IEnumerable<ApiSong> songs) : base(songs)
{
HasMoreItems = true;
}
public bool HasMoreItems { get; set; }
private bool isRunning = false;
public Windows.Foundation.IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
{
if (isRunning) //not thread safe
{
throw new InvalidOperationException("Only one operation in flight at a time");
}
isRunning = true;
return AsyncInfo.Run(async c => {
var api = App.container.Resolve<ApiMan>();
var url = this.Last().PlaylistUrl;
var currentPageNum = GetCurrentPageNumber(url);
var songs = new ObservableCollection<ApiSong>();
try
{
songs = await api.GetSongs(url, "", 45, currentPageNum);
}
catch
{
HasMoreItems = false;
}
foreach (var s in songs)
{
if (!this.Any(x => x.itemid == s.itemid))
{
this.Add(s);
}
else
{
Debug.WriteLine("avoiding duplicate");
}
}
HasMoreItems = songs.Any();
isRunning = false;
return new LoadMoreItemsResult() {
Count = (uint)songs.Count
};
});
}
}
namespace HypeM8.ViewModels
{
class MoreViewmodel
{
private string _playlistUrl;
public MoreViewmodel(string playlistUrl, string title)
{
_playlistUrl = playlistUrl;
Popular = new ScrollableObservableCollection(GetItems());
}
private ObservableCollection<ApiSong> _popular;
public ObservableCollection<ApiSong> Popular
{
get { return _popular; }
set { _popular = value; NotifyPropertyChanged(); }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment