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
- Replace
<ListView> / <ListView.ItemTemplate> with <CollectionView> / <CollectionView.ItemTemplate> in MainPage.xaml, removing the <ViewCell> wrapper.
- Add
SelectionMode="Single" and replace ItemTapped="OnListItemTapped" with SelectionChanged="OnListItemTapped".
- 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
Summary
Discovered while adding Windows CI coverage for
TodoApp.MAUIin #510 (PR #514,build-samples-todoapp-windows.yml). Building the sample produces aCS0618obsolete-API warning:Root cause
TodoApp.MAUIstill uses the deprecatedMicrosoft.Maui.Controls.ListViewcontrol: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.ListViewhas been marked[Obsolete]in .NET MAUI in favor ofCollectionView, which is what triggersCS0618at both the XAML-generated partial class usage and the code-behind cast.Additional information needed / design decisions
CollectionViewdoesn't have a directItemTappedevent — it usesSelectionChangedwith aSelectionMode(Single/Multiple), so the migration isn't a pure find-and-replace and needs a decision on the interaction model:SelectionMode="Single"+SelectionChangedand resetSelectedItem = nullafterward to preserve the current "tap to act, then deselect" behavior, orTapGestureRecognizerper 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).CollectionViewhas different default sizing/virtualization behavior thanListView.Suggested fix
<ListView>/<ListView.ItemTemplate>with<CollectionView>/<CollectionView.ItemTemplate>inMainPage.xaml, removing the<ViewCell>wrapper.SelectionMode="Single"and replaceItemTapped="OnListItemTapped"withSelectionChanged="OnListItemTapped".OnListItemTappedinMainPage.xaml.csto useSelectionChangedEventArgs(e.CurrentSelection) instead ofItemTappedEventArgs, and replace thesender is ListView itemListcast with theCollectionViewequivalent (sender is CollectionView itemList/itemList.SelectedItem = null).Related
/samples, where this warning was first surfaced (no CI previously built this project at all).