Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Crash removing item from CarouselView - fix #22107

Merged
merged 3 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue19786">
<StackLayout>
<CarouselView Position="{Binding Position}" HeightRequest="200" ItemsSource="{Binding Items}">
<CarouselView.ItemTemplate>
<DataTemplate>
<Frame Margin="10" WidthRequest="200" BackgroundColor="Red">
<Label Text="{Binding .}"/>
</Frame>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>

<Grid ColumnDefinitions="*,*,*">
<Button AutomationId="addItemButton" Grid.Column="0" Text="Add item" Command="{Binding AddItemCommand}"/>
<Button AutomationId="goToNextItemButton" Grid.Column="1" Text="Go to next item" Command="{Binding GoToNextItemCommand}"/>
<Button AutomationId="removeLastItemButton" Grid.Column="2" Text="Remove last item" Command="{Binding RemoveItemCommand}"/>
</Grid>
</StackLayout>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Collections.ObjectModel;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;

namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 19786, "[Android] Crash removing item from CarouselView", PlatformAffected.All)]
public partial class Issue19786 : ContentPage
{
public Command AddItemCommand { get; set; }
public Command RemoveItemCommand { get; set; }
public Command GoToNextItemCommand { get; set; }

private int _position;
public int Position
{
get => _position;
set
{
_position = value;
OnPropertyChanged();
}
}

private ObservableCollection<string> _items = new();
public ObservableCollection<string> Items
{
get => _items;
set
{
_items = value;
OnPropertyChanged();
}
}

public Issue19786()
{
InitializeComponent();

AddItemCommand = new Command(() =>
{
Items.Add(Items.Count.ToString());
});

RemoveItemCommand = new Command(() =>
{
if (Items.Count > 0)
Items.RemoveAt(Items.Count - 1);
});

GoToNextItemCommand = new Command(() =>
{
if (Position < Items.Count - 1)
Position++;
});

BindingContext = this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ public override void UpdateAdapter()

var oldItemViewAdapter = ItemsViewAdapter;

UnsubscribeCollectionItemsSourceChanged(ItemsViewAdapter);
if (oldItemViewAdapter != null && _initialized)
{
UnsubscribeCollectionItemsSourceChanged(oldItemViewAdapter);
ItemsView.SetValueFromRenderer(CarouselView.PositionProperty, 0);
ItemsView.SetValueFromRenderer(CarouselView.CurrentItemProperty, null);
}
Expand Down
31 changes: 31 additions & 0 deletions src/Controls/tests/UITests/Tests/Issues/Issue19786.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.AppiumTests.Issues
{
public class Issue19786 : _IssuesUITest
{
public Issue19786(TestDevice device) : base(device)
{
}

public override string Issue => "[Android] Crash removing item from CarouselView";

[Test]
[Category(UITestCategories.CarouselView)]
public void RemovingItemsShouldNotCauseCrash()
{
_ = App.WaitForElement("addItemButton");
App.Click("addItemButton");
App.Click("addItemButton");
App.Click("addItemButton");
App.Click("goToNextItemButton");
App.Click("goToNextItemButton");
App.Click("removeLastItemButton");
App.Click("removeLastItemButton");
App.Click("removeLastItemButton");
}

}
}
Loading