Skip to content

TodoApp.MAUI: Replace deprecated ListView with CollectionView (CS0618) #515

Description

@adrianhall

Summary

Discovered while adding Windows CI coverage for TodoApp.MAUI in #510 (PR #514, build-samples-todoapp-windows.yml). Building the sample produces a CS0618 obsolete-API warning:

MainPage.xaml.cs(34,23): warning CS0618: 'ListView' is obsolete: 'ListView is deprecated. Please use CollectionView instead.'

Root cause

TodoApp.MAUI still uses the deprecated Microsoft.Maui.Controls.ListView control:

  • samples/todoapp/TodoApp.MAUI/MainPage.xaml:24 — declares <ListView ItemTapped="OnListItemTapped" ItemsSource="{Binding Items}"> with a nested <ListView.ItemTemplate>/<DataTemplate>/<ViewCell>.
  • samples/todoapp/TodoApp.MAUI/MainPage.xaml.cs:34 — code-behind casts sender is ListView itemList inside OnListItemTapped to clear the selection after handling a tap.

ListView has been marked [Obsolete] in .NET MAUI in favor of CollectionView, which is what triggers CS0618 at both the XAML-generated partial class usage and the code-behind cast.

Additional information needed / design decisions

CollectionView doesn't have a direct ItemTapped event — it uses SelectionChanged with a SelectionMode (Single/Multiple), so the migration isn't a pure find-and-replace and needs a decision on the interaction model:

  • Use SelectionMode="Single" + SelectionChanged and reset SelectedItem = null afterward to preserve the current "tap to act, then deselect" behavior, or
  • Add a TapGestureRecognizer per item template if a non-selection-based tap is preferred.
  • <ViewCell> isn't used by CollectionView item templates — the DataTemplate content becomes the direct visual (no cell wrapper), so MainPage.xaml's item template will need restructuring (the ViewCell wrapper around the Grid should be removed).
  • Should be verified visually on at least one platform (Windows via the new CI, and ideally Android/iOS) since CollectionView has different default sizing/virtualization behavior than ListView.

Suggested fix

  1. Replace <ListView> / <ListView.ItemTemplate> with <CollectionView> / <CollectionView.ItemTemplate> in MainPage.xaml, removing the <ViewCell> wrapper.
  2. Add SelectionMode="Single" and replace ItemTapped="OnListItemTapped" with SelectionChanged="OnListItemTapped".
  3. Update OnListItemTapped in MainPage.xaml.cs to use SelectionChangedEventArgs (e.CurrentSelection) instead of ItemTappedEventArgs, and replace the sender is ListView itemList cast with the CollectionView equivalent (sender is CollectionView itemList / itemList.SelectedItem = null).

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions