0

I am making a UWP app. I have a ListView bound to an ObservableCollection. I want to find out when the end of ListView is reached. Once the end of ListView is reached, I have to make an API call to add more data to the List.

By end of ListView I mean that user has scrolled all the way down and I have to make a api call to get more data into the listview so that the user can scroll further.

How do I find out when the user has scrolled all the way down?

Vijay Nirmal
  • 5,239
  • 4
  • 26
  • 59
ravi kumar
  • 1,548
  • 1
  • 13
  • 47
  • Please refine your question, show us some code and let us know which part of the code you're having a hard time with. What do you consider to be `the end of the ListView reached`? The user scrolled all the way down? Or your code is done populating the ListView? – blaze_125 May 26 '17 at 17:46
  • @blaze_125 Added to the question. There's nothing much with the code, only a ListView which takes items from an ObservableCollection. – ravi kumar May 26 '17 at 17:57
  • Check this out to get started -> [handling-scroll-event-on-listview-in-c-sharp](https://stackoverflow.com/questions/1851620/handling-scroll-event-on-listview-in-c-sharp) It's not UWP specific but it may help you get going. – blaze_125 May 26 '17 at 18:15

2 Answers2

3

Use Incremental Loading Collection Helpers in UWP Community Toolkit

using Microsoft.Toolkit.Uwp;

public class Person
{
    public string Name { get; set; }
}
public class PeopleSource : IIncrementalSource<Person>
{
    private readonly List<Person> people;

    public async Task<IEnumerable<InfoOverView>> GetPagedItemsAsync(int pageIndex, int pageSize, CancellationToken cancellationToken = default(CancellationToken))
    {
        return AddItems();
    }

    public void AddItems()
    {
        people.Clear();
        //Code to add the additional items in the people List
        return people;
    }
}

//In Page.xaml.cs
public Page()
{
    this.InitializeComponent();
    var collection = new IncrementalLoadingCollection<PeopleSource, Person>();
    MasterDetailsViewPanel.ItemsSource = collection;
}
Vijay Nirmal
  • 5,239
  • 4
  • 26
  • 59
0

In the UWP Community Toolkit there's a special version of UWP ListView control called PullToRefreshListView that has a functional close to what you're looking for: when the user pulls down beyond the end of the list the RefreshRequested event is fired - this is where you add new items.

I suggest you go to the Windows Store and download UWP Community Toolkit Sample App to take a look at the demo of this control and have an example of code to start with.

A. Milto
  • 2,253
  • 1
  • 13
  • 19
  • 1
    you should have recommended [`Incremental Loading Collection Helpers`](http://www.uwpcommunitytoolkit.com/en/master/helpers/IncrementalLoadingCollection/) – Vijay Nirmal May 27 '17 at 06:54