# .NET MAUI CollectionView LoadOnDemand Collection

To load items on demand, you can use the `Telerik.Maui.LoadOnDemandCollection<T>` and set it as an `ItemsSource` for the `CollectionView`.

The `Telerik.Maui.LoadOnDemandCollection<T>` 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:

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

**2.** Define the CollectionView control:

```XAML
<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:

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

**4.** Create a `ViewModel` and use the `Telerik.Maui.LoadOnDemandCollection<T>` as a type of the property bound to the `RadCollectionView.ItemsSource`:

```C#
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:

![Telerik UI for .NET MAUI CollectionView loading additional Person items on demand from the built-in load on demand collection](https://www.telerik.com/maui-ui/documentation/assets/9228e53eed9e4fafef65254d6533a6c5/load-on-demand.gif "Telerik .NET MAUI CollectionView")

> For a runnable example demonstrating the CollectionView LoadOnDemand Collection, see the [SDKBrowser Demo Application](https://www.telerik.com/maui-ui/documentation/demos-and-sample-apps/sdkbrowser-app) and go to **CollectionView > Load On Demand** category.

## See Also

* [Load On Demand Command](https://www.telerik.com/maui-ui/documentation/controls/collectionview/load-on-demand/command)
* [Load On Demand Event](https://www.telerik.com/maui-ui/documentation/controls/collectionview/load-on-demand/event)
* [Customize the Load On Demand Manual and Automatic Loading Indicators](https://www.telerik.com/maui-ui/documentation/controls/collectionview/load-on-demand/templates)
