简体   繁体   English

向下滚动并更新 Listview C#

[英]Scroll down and Update Listview C#

I have a ListView in my UWP app.我的 UWP 应用中有一个 ListView。

This is my code:这是我的代码:

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

Now, how can I receive an event when this ListView scrolls to nearly end?现在,当这个 ListView 滚动到接近结束时,我如何接收事件? I want to Update ItemsSource to add new Items to bottom of the ListView.我想更新 ItemsSource 以将新项目添加到 ListView 的底部。

Welcome to StackOverflow!欢迎使用 StackOverflow! What you want to achieve is called Incremental Loading.您想要实现的称为增量加载。 You don't have to implement it by your self - just check existing solutions.您不必自己实施它 - 只需检查现有的解决方案。 Windows Community Toolkit has IncrementalLoadingCollection helpers that can help you. Windows Community Toolkit具有可以帮助您的IncrementalLoadingCollection帮助程序。 You can check demo in their sample app: Windows Community Toolkit Sample App in Windows Store (go to helpers > Incremental Loading Collection ).您可以在他们的示例应用程序中查看演示: Windows 应用商店中的 Windows 社区工具包示例应用程序(转到helpers > Incremental Loading Collection )。 Here is some sample code from this app:以下是此应用程序的一些示例代码:

// 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}'}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM