Rick Engle
Rick Engle

Reputation: 103

How to use the UWP Toolkit's Incremental Loading Collection helper for adding new data

in the UWP Toolkit Sample, in the Incremental Loading Collection scenario a List of type Person is created with 200 items and when demonstrating the helper it seems like when more items need to be read, then additional items are fetched from the existing list:

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;
}

But what about if you wanted to call an API to fetch new items and add them to the list? What I would like to do and get suggestion on how are:

  1. I start with a ListView that is populated with objects that I have filled based on an initial call to a web API which returns a JSON result. Generally only about 25 items at a time are returned.
  2. I’d like to be able to detect when a user is scrolling down that they have hit the end of the listview and more items need to be fetched
  3. Then when the end of the list is reached, make a call to the same API and fetch the next 25 and then add them to the ListView

I have a feeling this is pretty easy to do but I can’t figure out from the sample how to do steps 2 and 3.

Any suggestions would be great!!

Thanks, Rick

Upvotes: 0

Views: 512

Answers (1)

Shawn Kendrot
Shawn Kendrot

Reputation: 12465

You would need to change the GetPagesItemsAsync method to call your API and return the items

public async Task<IEnumerable<Person>> GetPagedItemsAsync(int pageIndex, int pageSize)
{
    // Call into your API classes to return a new collection
    var people = await PeopleClient.GetPeople(pageIndex);
    return people;
}

In this example the PeopleClient would be a class that uses the HttpClient class to make a HTTP call to some resource and return the result as a Person object.

Upvotes: 2

Related Questions