Repository navigation
TodoApp.MAUI: Replace deprecated ListView with CollectionView (CS0618) #515
Copy link
Copy link
Closed
Labels
Description
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 castssender is ListView itemListinsideOnListItemTappedto 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"+SelectionChangedand resetSelectedItem = nullafterward to preserve the current "tap to act, then deselect" behavior, or - Add a
TapGestureRecognizerper item template if a non-selection-based tap is preferred. <ViewCell>isn't used byCollectionViewitem templates — theDataTemplatecontent becomes the direct visual (no cell wrapper), soMainPage.xaml's item template will need restructuring (theViewCellwrapper around theGridshould be removed).- Should be verified visually on at least one platform (Windows via the new CI, and ideally Android/iOS) since
CollectionViewhas different default sizing/virtualization behavior thanListView.
Suggested fix
- Replace
<ListView>/<ListView.ItemTemplate>with<CollectionView>/<CollectionView.ItemTemplate>inMainPage.xaml, removing the<ViewCell>wrapper. - Add
SelectionMode="Single"and replaceItemTapped="OnListItemTapped"withSelectionChanged="OnListItemTapped". - Update
OnListItemTappedinMainPage.xaml.csto useSelectionChangedEventArgs(e.CurrentSelection) instead ofItemTappedEventArgs, and replace thesender is ListView itemListcast with theCollectionViewequivalent (sender is CollectionView itemList/itemList.SelectedItem = null).
Related
- Add CI workflow to build Windows-only /samples heads (Phase 2 of #509) #510 / PR ci: add build-samples workflow for Phase 2 Windows-only heads (#510) #514 — Phase 2 Windows CI for
/samples, where this warning was first surfaced (no CI previously built this project at all).
Reactions are currently unavailable
Activity
fix: replace deprecated ListView with CollectionView in TodoApp.MAUI (C…