I have a ListView in my Windows Phone 8.1 application and I can have something like 1000 or more results, so I need to implement the Load More function every time the scroll hits the bottom or some other logic and a natural way of initiating Add more items to the list.
I found that ListView supports ISupportIncrementalLoading and found this implementation: https://marcominerva.wordpress.com/2013/05/22/implementing-the-isupportincrementalloading-interface-in-a-window-store-app/ This was the best solution which I found, since it does not indicate a type, i.e. He is common.
My problem with this solution is that when the ListView is Loaded, LoadMoreItemsAsync runs all the time needed to get all the results, which means that the Load user does not start Load Load. I'm not sure what the LoadMoreItemsAsync trigger LoadMoreItemsAsync , but something is wrong because it assumes that this happens when I open the page and load all the items in place, without me doing anything or scrolling. Here's the implementation:
IncrementalLoadingCollection.cs
public interface IIncrementalSource<T> { Task<IEnumerable<T>> GetPagedItems(int pageIndex, int pageSize); void SetType(int type); } public class IncrementalLoadingCollection<T, I> : ObservableCollection<I>, ISupportIncrementalLoading where T : IIncrementalSource<I>, new() { private T source; private int itemsPerPage; private bool hasMoreItems; private int currentPage; public IncrementalLoadingCollection(int type, int itemsPerPage = 10) { this.source = new T(); this.source.SetType(type); this.itemsPerPage = itemsPerPage; this.hasMoreItems = true; } public bool HasMoreItems { get { return hasMoreItems; } } public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count) { var dispatcher = Window.Current.Dispatcher; return Task.Run<LoadMoreItemsResult>( async () => { uint resultCount = 0; var result = await source.GetPagedItems(currentPage++, itemsPerPage); if(result == null || result.Count() == 0) { hasMoreItems = false; } else { resultCount = (uint)result.Count(); await dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => { foreach(I item in result) this.Add(item); }); } return new LoadMoreItemsResult() { Count = resultCount }; }).AsAsyncOperation<LoadMoreItemsResult>(); } }
Here is PersonModelSource.cs
public class DatabaseNotificationModelSource : IIncrementalSource<DatabaseNotificationModel> { private ObservableCollection<DatabaseNotificationModel> notifications; private int _type = ""; public DatabaseNotificationModelSource() {
I changed it a bit because calling my database is asynchronous, and that was the only way I could wait for a request before populating the collection.
And in my DatabaseNotificationViewModel.cs
IncrementalNotificationsList = new IncrementalLoadingCollection<DatabaseNotificationModelSource, DatabaseNotificationModel>(type);
Everything works fine, except for the not so normal "Load More". What is wrong in my code?