xamlwinui-3winuiwindows-community-toolkitwindows-app-sdk

How to use IncrementalLoadingCollection in ItemsRepeater?


I am trying to use IncrementalLoadingCollection in ItemsRepeater. The GetPagedItemsAsync in the IIncrementalSource is not called automatically when scrolled . Should I manually trigger LoadMoreAsync in the scrollviewer_viewchanged ? . I cannot use GridView as I want UniformGridLayout. Is there any way to fix this?


Solution

  • Yes, you need to call LoadMoreItemsAsync() manually. ListView and GridView support ISupportIncrementalLoading when their ItemsSource implements it, but ItemsRepeater does not.

    Something like this should give some direction:

    // You might need to adjust (calculate?) this value
    // based on your UI layout and design.
    private uint ItemsCountPerPage { get; } = 10;
    
    private async void ScrollViewerControl_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
    {
        if (sender is not ScrollViewer scrollViewer)
        {
            return;
        }
    
        double difference = scrollViewer.ScrollableHeight - scrollViewer.VerticalOffset;
    
        if (difference < 100)
        {
            _ = await PeopleSource.LoadMoreItemsAsync(ItemsCountPerPage);
        }
    }
    
    private async void ScrollViewerControl_Loaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
    {
        _ = await PeopleSource.LoadMoreItemsAsync(ItemsCountPerPage);
    }