Download the source : http://aka.ms/q4mity
You could find in my last post http://aka.ms/islchg how to populate an collection of data in background. But in this post I would to explain how to sequentialy load a subset of data, in order to allow a fast & fluid scrolling loading an huge set of data.
First the class has to implement the ISupportIncrementalLoading, IList and INofifyCollectionChanged. For a simplier sample, it’s possible to inherit from the ObservableCollection<T> class instead of the latter two interfaces
The CCTOR take a delegate as an argument, in order to retrieve a subset of data. This is what you will have to implement.
Two methods to implement
HasMoreItems, is invoked by the control (GridView or ListView), to know if more items has to be loaded.
LoadMoreItemsAsync, is invoked with the variable count argument. This argument is calculated according to several factors :
First, the control need to know the number of objects to virtualize. So count is always equal 1 for the first call to the method.
Then, for an object with a size of 480*680, on a 15 inch screen, with a 1920*1080 resolution, XAML virtualize 24 objects (4 visible objects as you can see on the following picture) , count=24.
By the way, using the DataFetchSize and IncrementalLoadingThreshold attributes, we are able to bypass this value for the same config.
With a DataFetchSize=1, the count is equal 16
With DataFetchSize=5 count=40 and so on.
With IncrementalLoadingThreshold=10, LoadMoreItemsAsync will be call 3 times with 120 virtualized objects (3*40)
It’s up to you to play with these attributes, in order to find the more accurate setting for your application.
With the code attach with this post you could play with sliders to see what’s happened when you use different values.
Now go deeper in the LoadMoreItemsAsync
The return parameter is an IAsyncOperation<LoadMoreItemsResult>. Interface. This interface, is create from the AsyncInfo.Run available in the System.Runtime.InteropServices.WindowsRuntime, with a lambda expression as a parameter. This lambda call the InternalLoadMoreItemsAsync()
The IntenalLoadMoreItemsAsync() goals are :
In order to use this collection, you just has to create your own data model.
Then implement an algorithm wich populate the data.
In this sample, I pushed 9561 pictures on Azure ( http://devosaure.blob.core.windows.net/images/) and build a really simple algorithm based on the picture name .
You could find in the source code (http://aka.ms/q4mity) :
To play with this sample, click right to show the AppBar, and select the button you want.
You will notice it’s possible to disable the virtualisation, with the IncrementalLoadingTrigger = IncrementalLoadingTrigger.None. In this case the GridView doesn’t call the LoadMoreItemsAsync() method but it’s possible to invoke manually the LoadMoreItems depending on what you want for you application.