New to Telerik UI for .NET MAUI? Start a free 30-day trial

.NET MAUI CollectionView LoadOnDemand Collection

To load items on demand, you can use the RadCollectionView.LoadOnDemandCollection and set it as an ItemsSource for the CollectionView.

The LoadOnDemandCollection is a generic type, so you need to point the type of objects it will contain. The type extends the ObservableCollection<T> class and expects a Func<CancellationToken, IEnumerable> in the constructor.

Example

The following example demonstrates a simple setup that shows how to use the collection:

1. Create a sample model:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public Gender Gender { get; set; }
}

2. Define the CollectionView control:

<telerik:RadCollectionView ItemsSource="{Binding Items}"
                           DisplayMemberPath="Name"
                           LoadOnDemandBufferItemsCount="8"
                           IsLoadOnDemandEnabled="True">
    <telerik:RadCollectionView.BindingContext>
        <local:LoadOnDemandCollectionViewModel />
    </telerik:RadCollectionView.BindingContext>
</telerik:RadCollectionView>

3. Add the telerik namespace:

xmlns:telerik="http://schemas.telerik.com/2022/xaml/maui"

4. Create a ViewModel and use the LoadOnDemandCollection as a type of the property bound to the RadCollectionView.ItemsSource:

public class LoadOnDemandCollectionViewModel : NotifyPropertyChangedBase
{
    public LoadOnDemandCollectionViewModel()
    {
        this.Items = new LoadOnDemandCollection<Person>(this.LoadMoreItems);

        for (int i = 0; i < 20; i++)
        {
            this.Items.Add(new Person { Name = "Person " + i, Age = i + 10 });
        }
    }

    public LoadOnDemandCollection<Person> Items { get; set; }
    private IEnumerable LoadMoreItems(CancellationToken cancellationToken)
    {
        List<Person> newItems = new List<Person>();
        int count = this.Items.Count;

        try
        {
            Thread.Sleep(2500);

            for (int i = 0; i < 10; i++)
            {
                newItems.Add(new Person { Name = $"Loaded item: Person {count + i}", Age = count + i + 10 });
            }

            return newItems;
        }
        catch (Exception)
        {
            return null;
        }
    }
}

This is the result:

.NET MAUI CollectionView LoadOnDemand Collection

For a runnable example demonstrating the CollectionView LoadOnDemand Collection, see the SDKBrowser Demo Application and go to CollectionView > Load On Demand category.

See Also

In this article