I am working with WinUI 3 in C# and trying to enable virtualization in a ListView so that only the necessary items are displayed. I want to use the IncrementalLoadingCollection from the Windows Community Toolkit, but I encountered an issue where it cannot be found.
Installed the latest version of the Windows Community Toolkit NuGet package.(CommunityToolkit.WinUI.Helpers) However, when I attempt to use IncrementalLoadingCollection, the compiler shows an error indicating it is not recognized.
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using CommunityToolkit.Common.Collections;
using CommunityToolkit.WinUI;
using System.Threading.Tasks;
using System.Threading;
using CommunityToolkit.WinUI.Helpers;
public sealed partial class SampleControl : UserControl
{
public SampleControl()
{
this.InitializeComponent();
// Error
var collection = new IncrementalLoadingCollection<SampleDataIncrementalSource, SampleData>();
MyListView.ItemsSource = collection;
}
}
public class SampleData
{
public string data1 { get; set; }
public string data2 { get; set; }
public SampleData( string _data1, string _data2 )
{
this.data1 = _data1;
this.data2 = _data2;
}
}
public class SampleDataIncrementalSource : IIncrementalSource<SampleData>
{
private int _currentPage = 0;
private const int _pageSize = 20;
public async Task<IEnumerable<SampleData>> GetPagedItemsAsync( int pageIndex, int pageSize )
{
await Task.Delay( 0 );
var items = new List<SampleData>();
for( int i = 0; i < pageSize; i++ )
{
items.Add( new SampleData(
$"Data1-{pageIndex * pageSize + i + 1}",
$"Data2-{pageIndex * pageSize + i + 1}" ) );
}
return items;
}
public async Task<IEnumerable<SampleData>> GetPagedItemsAsync( Int32 pageIndex, Int32 pageSize, CancellationToken cancellationToken = default )
{
var items = await GetPagedItemsAsync( _currentPage, _pageSize );
_currentPage++;
return items;
}
}
Make sure you installed the right NuGet package, CommunityToolkit.WinUI.Collections in this case. You can check it in the Windows Community Toolkit Gallery app.
Also, in the project file (*.csproj), edit the target versions to 22621.
<TargetFramework>net9.0-windows10.0.22621.0</TargetFramework>
You might also need this there:
<WindowsSdkPackageVersion>10.0.22621.38</WindowsSdkPackageVersion>
You can find more about this in the release notes.