我已經實現了一個ISupportIncrementalLoading
接口來執行ListView
的增量加載。ISupportIncrementalLoading檢索異常
該接口具有下面的代碼:
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
(或者至少對我來說最有意義的假設......)。
我很迷茫,我不知道如何解決它,如果任何人都可以幫助它,將不勝感激。
在此先感謝!
我可以推薦這個樣本嗎? https://github.com/Windows-XAML/Template10/tree/master/Samples/IncrementalLoading/IncrementalLoading –