Incremental Loading Collection Helpers

The IncrementalLoadingCollection helpers greatly simplify the definition and usage of collections whose items can be loaded incrementally only when needed by the view, i.e., when user scrolls a ListView or a GridView.

Helper Purpose
IIncrementalSource An interface that represents a data source whose items can be loaded incrementally.
IncrementalLoadingCollection An extension of ObservableCollection such that its items are loaded only when needed.

Example

IIncrementalSource allows to define the data source:

    // Be sure to include the using at the top of the file:
    //using Microsoft.Toolkit.Uwp;

    public class Person
    {
        public string Name { get; set; }
    }

    public class PeopleSource : IIncrementalSource<Person>
    {
        private readonly List<Person> people;

        public PeopleSource()
        {
            // Creates an example collection.
            people = new List<Person>();

            for (int i = 1; i <= 200; i++)
            {
                var p = new Person { Name = "Person " + i };
                people.Add(p);
            }
        }

        public async Task<IEnumerable<Person>> GetPagedItemsAsync(int pageIndex, int pageSize)
        {
            // Gets items from the collection according to pageIndex and pageSize parameters.
            var result = (from p in people
                            select p).Skip(pageIndex * pageSize).Take(pageSize);

            // Simulates a longer request...
            await Task.Delay(1000);

            return result;
        }
    }

The GetPagedItemsAsync method is invoked everytime the view need to show more items.

IncrementalLoadingCollection can then be bound to a ListView or a GridView-like control:

    var collection = new IncrementalLoadingCollection<PeopleSource, Person>();
    PeopleListView.ItemsSource = collection;

The IncrementalLoadingCollection constructor accepts the following arguments:

Name Description Type
source An implementation of the IIncrementalSource interface that contains the logic to actually load data incrementally. If the source isn't provided to the constructor, it is created automatically. IIncrementalSource
itemsPerPage The number of items to retrieve for each call. Default is 20. Integer
onStartLoading (optional) An Action that is called when a retrieval operation begins. Action
onEndLoading (optional) An Action that is called when a retrieval operation ends. Action
onError (optional) An Action that is called if an error occours during data retrieval. Action

Requirements (Windows 10 Device Family)

Device family Universal, 10.0.10586.0 or higher
Namespace Microsoft.Toolkit.Uwp

API