In this tutorial I will show how to use incrementally load ListView items in a Windows Phone 8.1 WinRT app.
Here are the steps.
Step 1: Add a ListView in your page
Add a ListView and define its DataTemplate in your page.
Step 2: Get the ListView’s ScrollViewer
In windows phone 8.1 ListView we cannot get reference of the ScrollViewer. You can find the ScrollViewer of your ListView by using VisualTreeHelper. Get the ScrollViewer using the following function.
Step 3: Add ListView Loaded event handler
In ListView’s Loaded event handler subscribe to the ScrollViewer‘s view changed event handler.
Step 4: Fetch more items in the View Changed event handler
In the view changed event handler of the ScrollViewer calculate the scroll progress and fetch more items if the progress is more than a certain threshold. In our case we call the fetchCountries function when the progress is more than 0.7. Also incall is set to true so that multiple calls at the same time can be avoided.
Our function fetchCountries adds 20 items every time it is called. The function sets the variable incall to false after all items have been added. Also endoflist is set to true when no more items are left.
Step 5: Add Main Grid Loaded function
In the Page’s main grid loaded function set the ListView’s ItemSource and call the fetch function to add initial items.
In order to build a Scrolling Horizontal menu bar in Windows phone app you need to disable the VerticalScrollbarVisibility and should set the HorizontalScrollbarVisibility to Auto or Hidden as according to your wish. An example is given below. HorizontalScrollbarVisibility.Hidden
We will be creating a horizontal scrolling list of different images placed inside a Scroll viewer. The code for the following is given below.