Search code examples
c#windows-runtimescroll-paging

Winrt implementation of ISupportIncrementalLoading


On my view I have a GridView. As the number of items can be very high I'm trying to implement ISupportIncrementalLoading as new ItemsSource.

public class IncrementalCollection : ObservableCollection<Object>, ISupportIncrementalLoading
    {
        private int _addedItems = 0;
        private const int _PAGESIZE = 20;

        private IList<BookingDTO> _bookings;

        public IncrementalCollection(Guid guid)
        {
            LoadBookings(guid);
    }

    private async Task LoadBookings(Guid guid)
    {
        var data = await IoC.Resolve<IMBAMService>().GetOrderedBookingsForAccountAsync(guid);
        _bookings = data.Data;
    }

    public bool HasMoreItems
    {
        get { return (this.Count < _bookings.Count); }
    }

    public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
    {
        var coreDispatcher = Window.Current.Dispatcher;

        return Task.Run<LoadMoreItemsResult>(async () =>
        {
            await coreDispatcher.RunAsync(CoreDispatcherPriority.High,
                () =>
                {
                    foreach (var item in _bookings.Skip(_addedItems).Take((int)count))
                    {
                        _addedItems++;
                        this.Add(item);
                    }
                });
            return new LoadMoreItemsResult() { Count = count };
        }).AsAsyncOperation<LoadMoreItemsResult>();
    }
}

In the navigation function I create a new instance.

BookingList.ItemsSource = new IncrementalCollection(ViewModel.Account.Id);BookingList.ItemsSource = new IncrementalCollection(Guid);

My problem is now that LoadMoreItemsAsync is called so many times that the hole list will be displayed and not as expected after scrolling.

What do I need to change that it loads only the first 50 items and the rest when it's needed after scrolling?

I've tried to implement it like here: http://blogs.msdn.com/b/devosaure/archive/2012/10/15/isupportincrementalloading-loading-a-subsets-of-data.aspx


Solution

  • It's possible that the GridView is trying to make use of infinite space instead of limiting its size to that of the visible screen. This would cause it to keep querying to load items to fill up the available space, even if those items are not visible. Read this page for more info on this, specifically where it mentions ScrollViewer: http://msdn.microsoft.com/en-us/library/windows/apps/hh994637.aspx.