I am trying to create a link to link to Win8 Metro for virtualized lists. While doing some (very rare) readings, I found that the recommended way to support this is through the interface ISupportIncrementalLoading.
I have a problem with my reference application where the method LoadMoreItemsAsyncis called once but not called again, although my property is HasmoreItemshardcoded to return True.
What the code below should do is load 40 elements and then load the “x” number at a time. What happens is that it downloads the first 40 items, then it is prompted to download another 42, and then the download is no longer requested.
Here are the relevant parts of my code:
Xaml
<Grid Background="{StaticResource ApplicationPageBackgroundBrush}">
<ListView ItemsSource="{Binding Items}" Width="800" HorizontalAlignment="Left" Margin="12">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title}" Foreground="White"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
ViewModel and support classes:
public class MainViewModel : ViewModelBase
{
public MyIncrementalCollection Items { get; set; }
public MainViewModel()
{
Items = new MyIncrementalCollection();
for (int i = 0; i < 40; i++)
{
Items.Add(new MyData {Title = string.Format("Item: {0}", i)});
}
}
}
public class MyData
{
public string Title { get; set; }
}
public class MyIncrementalCollection : ObservableCollection<MyData>, ISupportIncrementalLoading
{
public bool HasMoreItems
{
get
{
return true;
}
}
public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
{
return
(IAsyncOperation<LoadMoreItemsResult>)
AsyncInfo.Run((System.Threading.CancellationToken ct) => LoadDataAsync(count));
}
private async Task<LoadMoreItemsResult> LoadDataAsync(uint count)
{
for (int i = 0; i < count; i++)
{
this.Add(new MyData { Title = string.Format("Item: {0}, {1}", i, System.DateTime.Now) });
}
var ret = new LoadMoreItemsResult {Count = count};
return ret;
}
}
}