【问题标题】:Scroll down and Update Listview C#向下滚动并更新 Listview C#
【发布时间】:2019-06-14 00:44:44
【问题描述】:

我的 UWP 应用中有一个 ListView。

这是我的代码:

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

现在,当这个 ListView 滚动到几乎结束时,我如何接收事件?我想更新 ItemsSource 以将新项目添加到 ListView 的底部。

【问题讨论】:

    标签: c# xaml listview uwp


    【解决方案1】:

    欢迎来到 StackOverflow! 您想要实现的称为增量加载。您不必自己实施它 - 只需检查现有的解决方案。 Windows 社区工具包IncrementalLoadingCollection 可以帮助您的助手。您可以在他们的示例应用程序中查看演示:Windows Community Toolkit Sample App in Windows Store(转到 helpers > Incremental Loading Collection)。以下是此应用程序的一些示例代码:

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

    【讨论】:

    • 谢谢。我得到服务器的项目。每个请求都给我 20 件物品。那么我如何滚动到结尾,发送另一个请求并将新项目添加到_people
    • IIncrementalSource 具有称为 GetPagedItemsAsync 的方法。这是您应该请求更多项目的地方。您必须创建适合您的案例的自己的源。
    • 另外请注意,这里的 PeopleSource 实例是 ItemsSource。 _people 变量只是一个辅助变量,用于模拟真实的数据源(如服务器)。
    • 是的,我知道,但我不想先得到所有请求。我需要有一个事件,当滚动结束时,我会在那个时候向服务器发送一个请求。并添加到 _people 您的代码仅用于 UX 我需要一个事件来向服务器发送请求并获取新的 20 个其他项目
    • GetPagedItemsAsync 是您的“事件”。如果将 ItemsSource 设置为自定义源(例如 PeopleSource),则在 ListView 滚动到底部时将调用 GetPagedItemsAsync。从 GetPagedItemsAsync 返回的内容会自动添加到 ListView 的底部。
    猜你喜欢
    • 2019-06-27
    • 1970-01-01
    • 2014-10-06
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多