在我滚动 GridView 之前,ISupportIncrementalLoading 不起作用

ISupportIncrementalLoading does not work until I scroll in GridView

我已经实现了 ISupportIncrementalLoading 以在用户滚动时加载列表中的大量项目。当我加载视图时,它是空的,它只会加载更多数据,直到我滚动列表。

这是实现它的 class

  public class DeviceListDataSource<T> : ObservableCollection<T>, ISupportIncrementalLoading
    where T : class
{
    public int LastItem = 0;

    public bool HasMoreItems => hasMoreItems;

    private int currentPage;

    private List<T> source;

    private bool hasMoreItems;

    public DeviceListDataSource(List<T> source)
    {
        this.source = source;
        hasMoreItems = true;
    }

    public async Task<IEnumerable<T>> GetPagedItems(int pageIndex, int pageSize)
    {
        return await Task.Run(() =>
        {
            var result = (from p in source
                          select p).Skip(pageIndex * pageSize).Take(pageSize);

            return result;
        });

    }

    public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
    {
        var dispatcher = Window.Current.Dispatcher;
        return Task.Run(
       async () =>
       {
           var result = await GetPagedItems(currentPage++, (int) count);

           if (result == null || result.Count() == 0)
           {
               hasMoreItems = false;
           }
           else
           {

               await dispatcher.RunAsync(
                   CoreDispatcherPriority.Normal,
                   () =>
                   {
                       foreach (T item in result)
                       {
                           Add(item);
                       }
                   });
           }

           return new LoadMoreItemsResult() { Count = (uint)currentPage };

       }).AsAsyncOperation();
    }
}

}

并且在视图模型中,我将其分配给绑定到 xaml 视图中的列表视图的变量:Items = new DeviceListDataSource<DataObject>(source);

有没有办法加载列表中的前 X 项?它在第一次加载时始终为 0,并且在我滚动之前一直保持这种状态。非常感谢!

Is there a way to load the first X items in the list?

您已经在 DeviceListDataSource class 中实现了 LoadMoreItemsAsync 方法,只需在初始加载时调用此方法以手动加载 X 项代码。例如:

DeviceListDataSource<Data> Items = new DeviceListDataSource<Data>(source);
Items.LoadMoreItemsAsync(10);