我已经实现了一个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;其中,LiveText是一个UserForm,LiveTextCode是一个类,除了其他功能之外,它还设置了前一个UserForm。
UserForm是通过读取位于服务器中的XML文件来填充的,因此代码必须执行async操作,为此,包含范围也必须是。由于未知的原因,自定义接口的实例在填充之前被调用,因此,我得到了一个NullReferenceException (或者至少对我来说最有意义的假设...)。
我迷路了,我不知道如何修复它,如果有人能帮上忙,我将不胜感激。
提前感谢!
发布于 2016-04-04 12:38:47
不使用this.LTextLW.ItemsSource = collection;
指定一个ObservableCollection项,比如collection。现在,通过将它绑定到您的ItemsSource="{Binding collection}",将它绑定到您的listview。
因为它是一个ObservableCollection类型,只要你的集合值被更新,它也会反映在你的视图中。
否则,您还可以使用RaisePropertyChanged事件指定集合
private IncrementalLoadingCollection<LiveTextCode, LiveText> _collection;
public IncrementalLoadingCollection<LiveTextCode, LiveText> collection
{
get { return _collection; }
set
{
_collection = value;
RaisePropertyChanged();
}
}这将在值发生变化时处理UI的更新。
https://stackoverflow.com/questions/34226344
复制相似问题