不调用LoadMoreItemsAsync

时间:2017-11-20 07:58:54

标签: c# uwp

我正在尝试在ListView中实现延迟加载项目。 但LoadMoreItemsAsync永远不会被召唤。

XAML:

<Grid>
    <ListView Grid.Row="1" x:Name="MasterListView" ItemsSource="{x:Bind ViewModel.News}">
        <!--
            <ListView.ItemTemplate>
        -->
    </ListView>
</Grid>

视图模型:

public class HomeViewModel
{
    public HomeViewModel()
    {
        News = new PaginateCollection<NormalNews>(LoadNews);
    }

    public PaginateCollection<NormalNews> News { get; set; }

    public async Task<IEnumerable<NormalNews>> LoadNews(uint size)
    {
        // ...
    }
}

PaginateCollection:

public class PaginateCollection<T> : ObservableCollection<T>, ISupportIncrementalLoading
{
    Func<uint, Task<IEnumerable<T>>> load;
    public PaginateCollection(Func<uint, Task<IEnumerable<T>>> load)
    {
        this.load = load;
    }

    public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
    {
        return AsyncInfo.Run(async c =>
        {
            var data = await load(count);
            foreach (var item in data)
            {
                Add(item);
            }
            return new LoadMoreItemsResult
            {
                Count = (uint)data.Count()
            };
        });
    }

    public bool HasMoreItems { get; set; }
}

1 个答案:

答案 0 :(得分:0)

在PaginateCollection的构造函数中,将HasMoreItems属性设置为true。这将确保第一个加载触发器。因此,将调用方法LoadNews()并运行您的逻辑。

public PaginateCollection(Func<uint, Task<IEnumerable<T>>> load)
{
    this.load = load;
    HasMoreItems = true;
}