Last active
January 12, 2016 23:54
-
-
Save lawrencegripper/9396993 to your computer and use it in GitHub Desktop.
Setup infinite scrolling for windows store with gridview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<GridView Grid.Row="1" ItemsSource="{Binding Popular}" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}; | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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