Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Fix crash adding items to CarouselView on Android (#13617) fixes #13616
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuarezruiz authored Feb 3, 2021
1 parent 48e434b commit cbd89a3
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="utf-8" ?>
<local:TestContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Test 13616" xmlns:local="using:Xamarin.Forms.Controls"
x:Class="Xamarin.Forms.Controls.Issues.Issue13616">
<StackLayout>
<Label
Padding="12"
BackgroundColor="Black"
TextColor="White"
Text="Tap the Button to add a new Item, without exceptions, the test has passed."/>
<StackLayout Padding="20,40,20,20">
<Button
AutomationId="AddItemButtonId"
Text="Add new Item"
CornerRadius="10"
Command="{Binding AddCardCommand}"
HeightRequest="40"
WidthRequest="200"
HorizontalOptions="CenterAndExpand"/>
<CarouselView
x:Name="carouselView"
AutomationId="CarouselViewId"
HorizontalScrollBarVisibility="Never"
ItemsSource="{Binding Items}"
PeekAreaInsets="15"
Loop="False"
HeightRequest="250"
VerticalOptions="Start">
<CarouselView.ItemsLayout>
<LinearItemsLayout
Orientation="Horizontal"
SnapPointsAlignment="Center"
SnapPointsType="MandatorySingle"
ItemSpacing="6"/>
</CarouselView.ItemsLayout>
<CarouselView.ItemTemplate>
<DataTemplate>
<Frame
HeightRequest="250"
CornerRadius="10"
HasShadow="False"
Padding="0"
IsClippedToBounds="True"
Margin="10">
<Grid
BackgroundColor="{Binding Color}">
<Grid Margin="20">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label
Grid.Row="0"
Margin="0,0,0,20"
Text="{Binding Name}"
TextColor="Black" />
<Label
Grid.Row="1"
Margin="0,0,0,20"
Text="{Binding Desc}"
TextColor="Black" />
</Grid>
</Grid>
</Frame>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
</StackLayout>
</StackLayout>
</local:TestContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
using Xamarin.Forms.Core.UITests;
#endif

namespace Xamarin.Forms.Controls.Issues
{
#if UITEST
[Category(UITestCategories.CarouselView)]
#endif
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 13616,
"[Bug] After updating XF 5.0.0.1931 getting Java.Lang.IllegalArgumentException: Invalid target position at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod",
PlatformAffected.Android)]
public partial class Issue13616 : TestContentPage
{
public Issue13616()
{
#if APP
InitializeComponent();
BindingContext = new Issue13616ViewModel();
#endif
}

protected override void Init()
{
}

#if UITEST && __ANDROID__
[Test]
public void Issue13616Test()
{
RunningApp.WaitForElement("AddItemButtonId");
RunningApp.Tap("AddItemButtonId");
RunningApp.WaitForElement("CarouselViewId");
}
#endif
}

[Preserve(AllMembers = true)]
public class Issue13616Model
{
public string Name { get; set; }
public string Desc { get; set; }
public Color Color { get; set; }
public double Scale { get; set; }
}

[Preserve(AllMembers = true)]
public class Issue13616ViewModel : BindableObject
{
int _i = 4;
ObservableCollection<Issue13616Model> _myList { get; set; } = new ObservableCollection<Issue13616Model>();

public Issue13616ViewModel()
{
Items = new ObservableCollection<Issue13616Model>
{
new Issue13616Model
{
Name = "Card 1",
Desc = "Card Holder Name 1",
Color = Color.Yellow
},
new Issue13616Model
{
Name = "Card 2",
Desc = "Card Holder Name 2",
Color = Color.Orange
},
new Issue13616Model
{
Name = "Card 3",
Desc = "Card Holder Name 3",
Color = Color.Red
}
};
}

public ObservableCollection<Issue13616Model> Items
{
get
{
return _myList;
}
set
{
_myList = value;
OnPropertyChanged(nameof(Items));
}
}

public ICommand AddCardCommand { get { return new Command(AddCard); } }

void AddCard()
{
var tempList = Items.ToList();
tempList.Add(new Issue13616Model
{
Name = "Card " + _i,
Desc = "Card Holder Name " + _i,
Color = Color.Blue
});
Items = new ObservableCollection<Issue13616Model>(tempList);
_i++;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1714,6 +1714,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue13436.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue8701.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13390.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13616.xaml.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Bugzilla22229.xaml">
Expand Down Expand Up @@ -2116,6 +2117,9 @@
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue13436.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue13616.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Bugzilla27417Xaml.xaml">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,15 @@ protected override void ScrollTo(ScrollToRequestEventArgs args)

if (_carouselViewLoopManager == null)
return;
//Special case here
//We could have a race condition where we are scrolling our collection to center the first item
//And at the same time the user is requesting we go to a particular item
if (position == -1 && Carousel.Loop)

// Special case here
// We could have a race condition where we are scrolling our collection to center the first item
// And at the same time the user is requesting we go to a particular item
if (position == -1)
{
_carouselViewLoopManager.AddPendingScrollTo(args);
if (Carousel.Loop)
_carouselViewLoopManager.AddPendingScrollTo(args);

return;
}

Expand Down

0 comments on commit cbd89a3

Please sign in to comment.