Skip to content

Commit 2fc9e3f

Browse files
BaY1251jsuarezruizjfversluis
authored
Fix BindingAdapterPosition Exception (#22676)
* Fix BindingAdapterPosition Exception * Added UITest * Run the test in all the platforms * Run the test in Android * Add test category --------- Co-authored-by: Javier Suárez <javiersuarezruiz@hotmail.com> Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>
1 parent 8f0f53e commit 2fc9e3f

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

src/Controls/src/Core/Handlers/Items/Android/Adapters/SelectableItemsViewAdapter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ bool PositionIsSelected(int position)
123123

124124
void SelectableClicked(object sender, int adapterPosition)
125125
{
126-
UpdateMauiSelection(adapterPosition);
126+
if (adapterPosition >= 0 && adapterPosition < ItemsSource?.Count)
127+
{
128+
UpdateMauiSelection(adapterPosition);
129+
}
127130
}
128131

129132
void UpdateMauiSelection(int adapterPosition)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
x:Class="Maui.Controls.Sample.Issues.Issue22674">
5+
<CollectionView
6+
AutomationId="TestCollectionView"
7+
SelectionMode="Single"
8+
ItemsSource="{Binding ItemList}"
9+
SelectionChanged="OnCollectionViewSelectionChanged">
10+
<CollectionView.ItemTemplate>
11+
<DataTemplate>
12+
<Label
13+
Text="{Binding .}"
14+
HorizontalTextAlignment="Center" />
15+
</DataTemplate>
16+
</CollectionView.ItemTemplate>
17+
</CollectionView>
18+
</ContentPage>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Collections.ObjectModel;
2+
using Microsoft.Maui.Controls;
3+
using Microsoft.Maui.Controls.Xaml;
4+
5+
namespace Maui.Controls.Sample.Issues
6+
{
7+
[XamlCompilation(XamlCompilationOptions.Compile)]
8+
[Issue(IssueTracker.Github, 22674, "Crash when quickly clicking to delete item", PlatformAffected.iOS)]
9+
public partial class Issue22674 : ContentPage
10+
{
11+
public Issue22674()
12+
{
13+
InitializeComponent();
14+
15+
BindingContext = this;
16+
}
17+
18+
public ObservableCollection<string> ItemList { get; } = ["A", "B", "C", "D", "E", "F", "G", "H", "I",];
19+
20+
void OnCollectionViewSelectionChanged(object sender, SelectionChangedEventArgs e)
21+
{
22+
if (sender is CollectionView view && view.SelectedItem is string item)
23+
{
24+
ItemList.Remove(item);
25+
}
26+
}
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#if ANDROID
2+
using NUnit.Framework;
3+
using UITest.Appium;
4+
using UITest.Core;
5+
6+
namespace Microsoft.Maui.TestCases.Tests.Issues;
7+
8+
public class Issue22674 : _IssuesUITest
9+
{
10+
public Issue22674(TestDevice device) : base(device) { }
11+
12+
public override string Issue => "Crash when quickly clicking to delete item";
13+
14+
[Test]
15+
[Category(UITestCategories.CollectionView)]
16+
public void RemoveItemWhenSelectionChanged()
17+
{
18+
App.WaitForElement("TestCollectionView");
19+
20+
for (int i = 0; i < 5; i++)
21+
{
22+
App.TapCoordinates(24, 24);
23+
}
24+
25+
// Without crashes, the test has passed.
26+
}
27+
}
28+
#endif

0 commit comments

Comments
 (0)