Skip to content

Commit f9fb282

Browse files
author
ahall
committed
fix: replace deprecated ListView with CollectionView in TodoApp.MAUI
Replaces the obsolete Microsoft.Maui.Controls.ListView (CS0618) with CollectionView in the TodoApp.MAUI sample's MainPage, removing the ViewCell wrapper the new control doesn't use and switching the tap handler from ItemTapped/ItemTappedEventArgs to SelectionChanged/SelectionChangedEventArgs (SelectionMode=Single, resetting SelectedItem afterward to preserve tap-then-deselect behavior). Also removes the now-unused implicit ListView style from Styles.xaml. Closes #515
1 parent 91be747 commit f9fb282

3 files changed

Lines changed: 24 additions & 28 deletions

File tree

samples/todoapp/TodoApp.MAUI/MainPage.xaml

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,30 @@
2121
</Grid.ColumnDefinitions>
2222

2323
<!-- List of items -->
24-
<ListView ItemTapped="OnListItemTapped" ItemsSource="{Binding Items}">
25-
<ListView.ItemTemplate>
24+
<CollectionView
25+
ItemsSource="{Binding Items}"
26+
SelectionChanged="OnListItemTapped"
27+
SelectionMode="Single">
28+
<CollectionView.ItemTemplate>
2629
<DataTemplate x:DataType="models:TodoItem">
27-
<ViewCell>
28-
<Grid Style="{StaticResource listItemContainer}">
29-
<Grid.RowDefinitions>
30-
<RowDefinition Height="Auto" />
31-
</Grid.RowDefinitions>
32-
<Grid.ColumnDefinitions>
33-
<ColumnDefinition Width="*" />
34-
<ColumnDefinition Width="Auto" />
35-
</Grid.ColumnDefinitions>
30+
<Grid Style="{StaticResource listItemContainer}">
31+
<Grid.RowDefinitions>
32+
<RowDefinition Height="Auto" />
33+
</Grid.RowDefinitions>
34+
<Grid.ColumnDefinitions>
35+
<ColumnDefinition Width="*" />
36+
<ColumnDefinition Width="Auto" />
37+
</Grid.ColumnDefinitions>
3638

37-
<Label Style="{StaticResource listItemTitle}" Text="{Binding Title}" />
38-
<Image
39-
Grid.Column="1"
40-
IsVisible="{Binding IsComplete}"
41-
Style="{StaticResource listItemIcon}" />
42-
</Grid>
43-
</ViewCell>
39+
<Label Style="{StaticResource listItemTitle}" Text="{Binding Title}" />
40+
<Image
41+
Grid.Column="1"
42+
IsVisible="{Binding IsComplete}"
43+
Style="{StaticResource listItemIcon}" />
44+
</Grid>
4445
</DataTemplate>
45-
</ListView.ItemTemplate>
46-
</ListView>
46+
</CollectionView.ItemTemplate>
47+
</CollectionView>
4748

4849
<Grid Grid.Row="1" ColumnDefinitions="*,Auto">
4950
<Border Grid.Column="0" Style="{StaticResource roundedCornerFrame}">

samples/todoapp/TodoApp.MAUI/MainPage.xaml.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ protected override void OnAppearing()
2424
this._viewModel.RefreshItemsCommand.Execute(null);
2525
}
2626

27-
public void OnListItemTapped(object sender, ItemTappedEventArgs e)
27+
public void OnListItemTapped(object sender, SelectionChangedEventArgs e)
2828
{
29-
if (e.Item is TodoItem item)
29+
if (e.CurrentSelection.FirstOrDefault() is TodoItem item)
3030
{
3131
this._viewModel.UpdateItemCommand.Execute(item.Id);
3232
}
3333

34-
if (sender is ListView itemList)
34+
if (sender is CollectionView itemList)
3535
{
3636
itemList.SelectedItem = null;
3737
}

samples/todoapp/TodoApp.MAUI/Resources/Styles/Styles.xaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,6 @@
278278
<Setter Property="HorizontalTextAlignment" Value="Center" />
279279
</Style>
280280

281-
<Style TargetType="ListView">
282-
<Setter Property="SeparatorColor" Value="{AppThemeBinding Light={StaticResource Gray200}, Dark={StaticResource Gray500}}" />
283-
<Setter Property="RefreshControlColor" Value="{AppThemeBinding Light={StaticResource Gray900}, Dark={StaticResource Gray200}}" />
284-
</Style>
285-
286281
<Style TargetType="Picker">
287282
<Setter Property="TextColor" Value="{AppThemeBinding Light={StaticResource Gray900}, Dark={StaticResource White}}" />
288283
<Setter Property="TitleColor" Value="{AppThemeBinding Light={StaticResource Gray900}, Dark={StaticResource Gray200}}" />

0 commit comments

Comments
 (0)