0

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?

Vijay Nirmal
  • 5,239
  • 4
  • 26
  • 59
  • Maybe something like pagination? Bind scroll event and when it reaches the end of that particular item just load another one. – mrogal.ski May 12 '17 at 11:17
  • @m.rogalski How to Bind scroll event? Can you please explain it with a code? – Vijay Nirmal May 12 '17 at 11:21
  • I haven't done it but from what I remember MasterDetailsView contains a ListView as a placeholder for each Item. And inside ListView you should have property called VerticalOffset. – mrogal.ski May 12 '17 at 11:34
  • My bad, Inside ListView you have to get first possible ScrollViewer : `GetDescendants().OfType().FirstOrDefault()` and this will have VerticalOffset property. – mrogal.ski May 12 '17 at 11:36
  • @m.rogalski Can you explain it in detail with a code? I have never used VerticalOffset property – Vijay Nirmal May 12 '17 at 11:42
  • UWP Community Toolkit has [IncrementalLoadingCollection](http://docs.uwpcommunitytoolkit.com/en/master/helpers/IncrementalLoadingCollection/) you should check that out. – AVK May 12 '17 at 15:46
  • @AVKNaidu To use IncrementalLoadingCollection we must have all the item in the collection. – Vijay Nirmal May 12 '17 at 15:54
  • `To use IncrementalLoadingCollection we must have all the item in the collection`, I have to disagree. If you see the `GetPagedItemsAsync`, You can always add items to the collection and then get the only items that are added. Check this [GitHub](https://github.com/avknaidu/IncrementalLoading) Repo. I Modified existing Example to add New Items on ScrollDown. – AVK May 12 '17 at 18:06
  • @AVKNaidu I will try and let you know the result – Vijay Nirmal May 12 '17 at 18:15
  • @AVKNaidu It works. I will answer my own question so that everyone else can learn. – Vijay Nirmal May 13 '17 at 07:44

1 Answers1

0

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;
}
Vijay Nirmal
  • 5,239
  • 4
  • 26
  • 59