您是否知道增量收集的替代方法,例如wpf ISupportIncrementalLoading?
答案 0 :(得分:0)
我认为没有任何答案;( 出于这个原因,我将在这里发布我的实现。 图书馆仍然是BETA版本,如果您有任何问题,请向您发送问题。
/// P.Zh.
public class IncrementalLoadingCollection<T, TT> : ObservableCollection<TT>, ISupportIncrementalLoading
where T : IIncrementalSource<TT>, new()
{
private T source;
private int itemsPerPage;
private bool hasMoreItems;
private int currentPage;
public IncrementalLoadingCollection(int itemsPerPage = 20)
{
this.source = new T();
this.itemsPerPage = itemsPerPage;
this.hasMoreItems = true;
}
public bool HasMoreItems
{
get { return hasMoreItems; }
}
public async Task<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
{
var task = Task.Factory.StartNew(async () =>
{
uint resultCount = 0;
var result = await source.GetPagedItems(null, currentPage++, itemsPerPage);
if (result == null || result.Count() == 0)
{
hasMoreItems = false;
}
else
{
resultCount = (uint)result.Count();
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
foreach (TT item in result)
this.Add(item);
});
}
return new LoadMoreItemsResult() { Count = resultCount };
}
// ,CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default
).Unwrap();
return task.Result;
}
}
public interface IIncrementalSource<T>
{
/// <summary>
/// Get Chunk of data in specific interval from a colection
/// </summary>
/// <param name="o">custome object</param>
/// <param name="pageIndex">uint start position</param>
/// <param name="pageSize">uint end position</param>
/// <returns>Result is chunk</returns>
Task<IEnumerable<T>> GetPagedItems(object o, int pageIndex, int pageSize);
bool HasMoreItems { get; set; }
}
public interface ISupportIncrementalLoading
{
bool HasMoreItems { get; }
Task<LoadMoreItemsResult> LoadMoreItemsAsync(uint count);
}
public interface IIncrementalResult<T>
{
IEnumerable<T> Items { get; }
int VirtualCount { get; }
}
public struct LoadMoreItemsResult
{
public uint Count { get; set; }
}