Search code examples
c#xamluwpwindows-community-toolkit

How to implement Incremental loading in MasterDetailsView?


I want to implement Incremental loading in MasterDetailsView. I know we can implement Incremental loading with ISupportIncremental​Loading. But one problem I don't have all the items in the ObservableCollection at once. The items in ObservableCollection will be added only when the user reach the end of the MasterDetailsView.ItemTemplate.

I have already created a function to load more Items in ObservableCollection but I want to call that function only when the user reaches the end of the MasterDetailsView.ItemTemplate.

So how do I do it?


Solution

  • We can implement Incremental loading with Incremental Loading Collection

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