mcpField
M
maui-collectionview

maui-collectionview

github.com/davidortinauDevelopment17664 installsUpdated 16w ago
Repository

About maui-collectionview

A .NET MAUI skill covering CollectionView patterns for scrollable data lists and grids. It focuses on bindings, refresh and incremental loading, along with layout and threading practices that avoid common UI and performance problems.

Key features

  1. 01Bind items with ObservableCollection
  2. 02Configure item templates without ViewCell
  3. 03Use SwipeView with ancestor bindings
  4. 04Add pull-to-refresh and incremental loading
  5. 05Choose layouts for virtualization
  6. 06Improve sizing and UI-thread updates

Use cases

  1. 01Create a scrollable list or grid of changing data
  2. 02Add swipe commands and pull-to-refresh
  3. 03Load additional items as a list is scrolled
  4. 04Diagnose blank items or slow scrolling

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/davidortinau/maui-skills --skill maui-collectionview

After installing, type/maui-collectionviewin Claude Code to confirm it loaded.

Bundled filesevals/evals.jsonreferences/collectionview-api.md

Details

From the project README

CollectionView – .NET MAUI

Use CollectionView for displaying scrollable lists and grids of data. It replaces ListView and offers better performance, flexible layouts, and no ViewCell requirement.

Essential patterns

Basic setup
<CollectionView ItemsSource="{Binding Items}">
    <CollectionView.ItemTemplate>
        <DataTemplate x:DataType="models:Item">
            <HorizontalStackLayout Padding="8" Spacing="8">
                <Image Source="{Binding Icon}" WidthRequest="40" HeightRequest="40" />
                <Label Text="{Binding Name}" VerticalOptions="Center" />
            </HorizontalStackLayout>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>
  • Bind ItemsSource to an ObservableCollection<T> so the UI updates on add/remove.
  • Each item template root must be a Layout or View — never use ViewCell.
  • Always set x:DataType on DataTemplate for compiled bindings.
SwipeView — binding from inside DataTemplate

Commands inside a DataTemplate can't directly reach your ViewModel. Use RelativeSource AncestorType:

<SwipeItem Text="Delete"
           BackgroundColor="Red"
           Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodels:MainViewModel}}, Path=DeleteCommand}"
           CommandParameter="{Binding}" />
Pull-to-refresh

Wrap CollectionView in a RefreshView. Set IsRefreshing back to false when done:

<RefreshView IsRefreshing="{Binding IsRefreshing}"
             Command="{Binding RefreshCommand}">
    <CollectionView ItemsSource="{Binding Items}" />
</RefreshView>
Incremental loading (infinite scroll)
<CollectionView ItemsSource="{Binding Items}"
                RemainingItemsThreshold="5"
                RemainingItemsThresholdReachedCommand="{Binding LoadMoreCommand}" />

⚠️ Do NOT use with StackLayout-based ItemsLayout — it has no virtualization and triggers infinite threshold-reached events. Always use LinearItemsLayout or GridItemsLayout.

Performance tips

  • Use MeasureFirstItem for uniform item sizes — significantly faster than the default MeasureAllItems:
    <LinearItemsLayout Orientation="Vertical" ItemSizingStrategy="MeasureFirstItem" />
    
  • Always use ObservableCollection<T>, not List<T>. Swapping a List forces a full re-render.
  • Update collections on the UI thread — MainThread.BeginInvokeOnMainThread(() => Items.Add(item)).

Common gotchas

IssueFix
UI doesn't update when items changeUse ObservableCollection<T>, not List<T>.
App crashes or blank itemsNever use ViewCell — use Grid, StackLayout, or any View as template root.
Items disappear or layout breaksAlways update ItemsSource and the collection on the UI thread (MainThread.BeginInvokeOnMainThread).
Incremental loading fires endlesslyDon't use StackLayout as layout; use LinearItemsLayout or GridItemsLayout.
EmptyView doesn't render correctlyWrap custom empty views in ContentView.
Poor scroll performanceUse MeasureFirstItem sizing strategy for uniform item sizes.
Selected state not visibleAdd VisualState Name="Selected" to the item template root element.
Binding errors in SwipeView commandsUse RelativeSource AncestorType to reach the ViewModel from inside the item template.

Comments

Markdown supported