1

我已經實現了一個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; 

LiveTextUserFormLiveTextCode是一類,其他功能中,設置以前的UserForm

UserForm是通過讀取位於服務器中的XML文件填充的,因此代碼必須執行async操作,因此,包含範圍也必須是。由於一個未知的原因,自定義界面的實例在填充之前被調用,所以我得到了NullReferenceException(或者至少對我來說最有意義的假設......)。

我很迷茫,我不知道如何解決它,如果任何人都可以幫助它,將不勝感激。

在此先感謝!

+1

我可以推薦這個樣本嗎? https://github.com/Windows-XAML/Template10/tree/master/Samples/IncrementalLoading/IncrementalLoading –

回答

0

而不是使用this.LTextLW.ItemsSource = collection;
指定ObservableCollection項目說collection。現在將它綁定到您的列表視圖,將其綁定到您的ItemsSource="{Binding collection}"
自您的集合值更新後,它的ObservableCollection類型將立即生效,因此它將反映在您的View中。
否則你也可以指定與RaisePropertyChanged事件

​​3210

這將處理UI每當值改變的更新用的集合。