title: Load more items with ListView (Windows 8.1)
body: <p>I'm using ListView to develop in XAML-C# a Windows Store App.</p>
  
  <p>I want to load more items on ListView every time the user arrived at the end of the list.</p>
  
  <p>I just read this discussion <a href="https://stackoverflow.com/questions/17237012/load-more-items-on-grid-view-scroll-end">Load more items on grid view scroll end</a> and I just read this example <a href="http://code.msdn.microsoft.com/windowsapps/Data-Binding-7b1d67b5" rel="nofollow noreferrer">http://code.msdn.microsoft.com/windowsapps/Data-Binding-7b1d67b5</a></p>
  
  <p>I tried to implement something like mentioned example:
  - Some of mycode</p>
  
  <pre><code>                uint nentries = 0;
  
              // Follow method return a int: number of items (entries) between 0 and 20
              nentries = await App.EntriesViewModel.LoadEntries(0);
              if (nentries != 0) 
              {
  //entries is a GeneratorIncrementalLoadingClass&lt;Entry&gt; 
                          App.EntriesViewModel.entries = new GeneratorIncrementalLoadingClass&lt;Entry&gt;(nentries, count =&gt;
                          {
                              //EntriesOc is an observable collection with INotifyPropertyChanged
                              return App.EntriesViewModel.EntriesOc.ElementAt(count);
                          });
    //entriesCVS is a CollectionViewSource defined into xaml code
                          entriesCVS.Source = App.EntriesViewModel.entries; 
                      }
  
              }
              this.DataContext = null;
              this.DataContext = App.EntriesViewModel;
  
               //until here it's works
  
              if (nentries == 20)
              {
                  uint n = 0;
                  while (nentries % 20 == 0)
                  {
                      n = await App.EntriesViewModel.LoadEntries(nentries);
                      if (n == 0) break; // no more data to load
                      nentries += n;
                      App.EntriesViewModel.entries = new GeneratorIncrementalLoadingClass&lt;Entry&gt;(nentries, (count) =&gt;
                      {
                          return App.EntriesViewModel.EntriesOc.ElementAt(count);
                      });
  
  
                      // without the follow line of code the CollectionViewSource doesn't update
                      // however the list scroll to the top (I want to remove this behaviour)
                      entriesCVS.Source = App.EntriesViewModel.entries; 
                  }
              }
  </code></pre>
  
  <ul>
  <li><p>IncrementalLoadingBase.cs (same file of example)</p>
  
  <blockquote>
    <p>namespace MySolution {</p>
  
  <pre><code>public abstract class IncrementalLoadingBase: IList, ISupportIncrementalLoading, INotifyCollectionChanged
  
  {
  
      public int Add(object value)
      {
          throw new NotImplementedException();
      }
  
      public void Clear()
      {
          throw new NotImplementedException();
      }
  
      public bool Contains(object value)
      {
          return _storage.Contains(value);
      }
  
      public int IndexOf(object value)
      {
          return _storage.IndexOf(value);
      }
  
      public void Insert(int index, object value)
      {
          throw new NotImplementedException();
      }
  
      public bool IsFixedSize
      {
          get { return false; }
      }
  
      public bool IsReadOnly
      {
          get { return true; }
      }
  
      public void Remove(object value)
      {
          throw new NotImplementedException();
      }
  
      public void RemoveAt(int index)
      {
          throw new NotImplementedException();
      }
  
      public object this[int index]
      {
          get
          {
              return _storage[index];
          }
          set
          {
              throw new NotImplementedException();
          }
      }
  
      public void CopyTo(Array array, int index)
      {
          ((IList)_storage).CopyTo(array, index);
      }
  
      public int Count
      {
          get { return _storage.Count; }
      }
  
      public bool IsSynchronized
      {
          get { return false; }
      }
  
      public object SyncRoot
      {
          get { throw new NotImplementedException(); }
      }
  
      public IEnumerator GetEnumerator()
      {
          return _storage.GetEnumerator();
      }
  
  
      public bool HasMoreItems
      {
          get { return HasMoreItemsOverride(); }
      }
  
      public Windows.Foundation.IAsyncOperation&lt;LoadMoreItemsResult&gt; LoadMoreItemsAsync(uint count)
      {
          if (_busy)
          {
              throw new InvalidOperationException("Only one operation in flight at a time");
          }
  
          _busy = true;
  
          return AsyncInfo.Run((c) =&gt; LoadMoreItemsAsync(c, count));
      }
  
  
      public event NotifyCollectionChangedEventHandler CollectionChanged;
  
  
      async Task&lt;LoadMoreItemsResult&gt; LoadMoreItemsAsync(CancellationToken c, uint count)
      {
          try
          {
              var items = await LoadMoreItemsOverrideAsync(c, count);
              var baseIndex = _storage.Count;
  
              _storage.AddRange(items);
  
              // Now notify of the new items
              NotifyOfInsertedItems(baseIndex, items.Count);
  
              return new LoadMoreItemsResult { Count = (uint)items.Count };
          }
          finally
          {
              _busy = false;
          }
      }
  
      void NotifyOfInsertedItems(int baseIndex, int count)
      {
          if (CollectionChanged == null)
          {
              return;
          }
  
          for (int i = 0; i &lt; count; i++)
          {
              var args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, _storage[i + baseIndex], i + baseIndex);
              CollectionChanged(this, args);
          }
      }
  
  
      protected abstract Task&lt;IList&lt;object&gt;&gt;LoadMoreItemsOverrideAsync(CancellationToken c, uint count);
      protected abstract bool HasMoreItemsOverride();
  
  
      List&lt;object&gt; _storage = new List&lt;object&gt;();
      bool _busy = false;
  
  }
  </code></pre>
    
    <p>}</p>
  </blockquote></li>
  <li><p>GeneratorIncrementalLoadingClass.cs (same file of example)</p></li>
  </ul>
  
  <blockquote>
  <pre><code>    namespace MySolution{
  </code></pre>
    
    <p>public class GeneratorIncrementalLoadingClass:
    IncrementalLoadingBase</p>
  
  <pre><code>    {
  </code></pre>
  </blockquote>
  
  <pre><code>        public GeneratorIncrementalLoadingClass(uint maxCount, Func&lt;int, T&gt; generator)
          {
  
              _generator = generator;
              _maxCount = maxCount;
          }
  
          protected async override Task&lt;IList&lt;object&gt;&gt; LoadMoreItemsOverrideAsync(System.Threading.CancellationToken c, uint count)
          {
              uint toGenerate = System.Math.Min(count, _maxCount - _count);
  
              // Wait for work 
              await Task.Delay(10);
  
              // This code simply generates
              var values = from j in Enumerable.Range((int)_count, (int)toGenerate)
                           select (object)_generator(j);
              _count += toGenerate;
  
              return values.ToArray();
          }
  
          protected override bool HasMoreItemsOverride()
          {
              return _count &lt; _maxCount;
          }
  
  
          Func&lt;int, T&gt; _generator;
          uint _count = 0;
          uint _maxCount;
  
      }
  }
  </code></pre>