【问题标题】:ISupportIncrementalLoading retrieving exceptionISupportIncrementalLoading 检索异常
【发布时间】:2016-03-17 12:46:02
【问题描述】:

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

界面代码如下:

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>();
        }
    }

接口的实例是这个:

var collection = new IncrementalLoadingCollection<LiveTextCode, LiveText>();
this.LTextLW.ItemsSource = collection;

其中LiveTextUserFormLiveTextCode 是一个类,除其他功能外,它还设置了以前的UserForm

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

我很迷茫,我不知道如何解决它,如果有人可以帮助它,将不胜感激。

提前致谢!

【问题讨论】:

标签: c# win-universal-app windows-10-universal windows-10-mobile uwp-xaml


【解决方案1】:

而不是使用this.LTextLW.ItemsSource = collection;
指定一个 ObservableCollection 项目,例如 collection。现在通过将其绑定到您的 ItemsSource="{Binding collection}" 将其绑定到您的列表视图。
由于它是 ObservableCollection 类型,因此一旦您的集合值更新,它也会反映在您的视图中。
否则,您还可以使用 RaisePropertyChanged 事件指定集合

private IncrementalLoadingCollection<LiveTextCode, LiveText> _collection;
public IncrementalLoadingCollection<LiveTextCode, LiveText> collection
        {
            get { return _collection; }
            set
            {
                _collection = value;
                RaisePropertyChanged();
            }
        }

这将在值更改时处理 UI 更新。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 2013-02-04
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多