首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ISupportIncrementalLoading检索异常

ISupportIncrementalLoading检索异常
EN

Stack Overflow用户
提问于 2015-12-11 22:56:47
回答 1查看 306关注 0票数 1

我已经实现了一个ISupportIncrementalLoading接口来执行ListView的增量加载。

该接口具有以下代码:

代码语言:javascript
复制
public interface IIncrementalSource<T>
    {
        Task<IEnumerable<T>> GetPagedItems(int pageIndex, int pageSize);
    }

    public class IncrementalLoadingCollection<T, I> : ObservableCollection<I>, 
        ISupportIncrementalLoading where T : IIncrementalSource<I>, new()
    {
        private T source;
        private int itemsPerPage;
        private bool hasMoreItems;
        private int currentPage;

        public IncrementalLoadingCollection(int itemsPerPage = 10)
        {
            this.source = new T();
            this.itemsPerPage = itemsPerPage;
            this.hasMoreItems = true;
        }

        public void UpdateItemsPerPage(int newItemsPerPage)
        {
            this.itemsPerPage = newItemsPerPage;
        }

        public bool HasMoreItems
        {
            get { return hasMoreItems; }
        }

        public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
        {

            return Task.Run<LoadMoreItemsResult>(
                async () =>
                {
                    uint resultCount = 0;
                    var dispatcher = Window.Current.Dispatcher;
                    var result = await source.GetPagedItems(currentPage++, itemsPerPage);

                    if(result == null || result.Count() == 0)
                    {
                        hasMoreItems = false;
                    } else
                    {
                        resultCount = (uint)result.Count();
                        await Task.WhenAll(Task.Delay(10), dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                        {
                            foreach (I item in result)
                                this.Add(item);
                        }).AsTask());
                    }



                    return new LoadMoreItemsResult() { Count = resultCount };

                }).AsAsyncOperation<LoadMoreItemsResult>();
        }
    }

接口的实例如下所示:

代码语言:javascript
复制
var collection = new IncrementalLoadingCollection<LiveTextCode, LiveText>();
this.LTextLW.ItemsSource = collection;

其中,LiveText是一个UserFormLiveTextCode是一个类,除了其他功能之外,它还设置了前一个UserForm

UserForm是通过读取位于服务器中的XML文件来填充的,因此代码必须执行async操作,为此,包含范围也必须是。由于未知的原因,自定义接口的实例在填充之前被调用,因此,我得到了一个NullReferenceException (或者至少对我来说最有意义的假设...)。

我迷路了,我不知道如何修复它,如果有人能帮上忙,我将不胜感激。

提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2016-04-04 12:38:47

不使用this.LTextLW.ItemsSource = collection;

指定一个ObservableCollection项,比如collection。现在,通过将它绑定到您的ItemsSource="{Binding collection}",将它绑定到您的listview。

因为它是一个ObservableCollection类型,只要你的集合值被更新,它也会反映在你的视图中。

否则,您还可以使用RaisePropertyChanged事件指定集合

代码语言:javascript
复制
private IncrementalLoadingCollection<LiveTextCode, LiveText> _collection;
public IncrementalLoadingCollection<LiveTextCode, LiveText> collection
        {
            get { return _collection; }
            set
            {
                _collection = value;
                RaisePropertyChanged();
            }
        }

这将在值发生变化时处理UI的更新。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34226344

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档