向下滚动并更新Listview C#

时间:2019-01-20 12:01:15

标签: c# xaml listview uwp

我的UWP应用中有一个ListView。

这是我的代码:

<ListView x:Name="viewMedias" SelectionMode="None" VirtualizingStackPanel.VirtualizationMode="Recycling" ScrollViewer.HorizontalScrollMode="Disabled" ScrollViewer.VerticalScrollMode="Auto">
        ......
</ListView>

现在,当此ListView滚动到接近尾声时,如何接收事件?我想更新ItemsSource以将新项目添加到ListView的底部。

1 个答案:

答案 0 :(得分:1)

欢迎使用StackOverflow! 您想要实现的目标称为增量加载。您不必自己实施它-只需检查现有解决方案即可。 Windows社区工具包具有IncrementalLoadingCollection个帮助程序,可以为您提供帮助。您可以在其示例应用程序中查看演示:Windows Community Toolkit Sample App in Windows Store(转到 helpers>增量加载集合)。这是该应用程序的一些示例代码:

// Defines a data source whose data can be loaded incrementally.
using Microsoft.Toolkit.Uwp;

public class Person
{
    public string Name { get; set; }
}

public class PeopleSource : IIncrementalSource<Person>
{
    private readonly List<Person> _people;

    public PeopleSource()
    {
        // Creates an example collection.
        _people = new List<Person>();

        for (int i = 1; i <= 200; i++)
        {
            var p = new Person { Name = "Person " + i };
            _people.Add(p);
        }
    }

    public async Task<IEnumerable<Person>> GetPagedItemsAsync(int pageIndex, int pageSize)
    {
        // Gets items from the collection according to pageIndex and pageSize parameters.
        var result = (from p in _people
                      select p).Skip(pageIndex * pageSize).Take(pageSize);

        // Simulates a longer request...
        await Task.Delay(1000);

        return result;
    }
}

// IncrementalLoadingCollection can be bound to a GridView or a ListView. In this case it is a ListView called PeopleListView.

var collection = new IncrementalLoadingCollection<PeopleSource, Person>();

PeopleListView.ItemsSource = collection;

// Binds the collection to the page DataContext in order to use its IsLoading and HasMoreItems properties.
DataContext = collection;

// XAML UI Element
<TextBlock Text="{Binding IsLoading, Converter={StaticResource StringFormatConverter}, ConverterParameter='Is Loading: {0}'}" />
<TextBlock Text="{Binding HasMoreItems, Converter={StaticResource StringFormatConverter}, ConverterParameter='Has More Items: {0}'}" />