[Android] Fixed a crash on CollectionView changing ItemsSource (#7945)

* Added repro sample

* Fixed the crash
This commit is contained in:
Javier Suárez Ruiz 2019-10-14 23:24:53 +02:00 коммит произвёл E.Z. Hart
Родитель 27879f653c
Коммит a47892feac
5 изменённых файлов: 162 добавлений и 2 удалений

Просмотреть файл

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8" ?>
<controls: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"
xmlns:controls="clr-namespace:Xamarin.Forms.Controls"
mc:Ignorable="d"
x:Class="Xamarin.Forms.Controls.Issues.Issue7943">
<ContentPage.Content>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid
Grid.Row="0"
BackgroundColor="Black">
<Label
TextColor="White"
Text="Press the buttons below to dynamically change the ItemTemplate and ItemsSource of the CollectionView. If change without problems, the test has passed."/>
</Grid>
<StackLayout
Grid.Row="1">
<Button
Text="Change Template"
Clicked="OnChangeTemplate"/>
<Button
Text="Change ItemsSource"
Clicked="OnChangeItemsSource"/>
<Button
Text="Clear ItemsSource"
Clicked="OnClearItemsSource"/>
</StackLayout>
<CollectionView
Grid.Row="2"
x:Name="collectionView">
<CollectionView.EmptyView>
<Grid
BackgroundColor="GreenYellow">
<Label
Text="No data available"
HorizontalOptions="Center"
VerticalOptions="Center"/>
</Grid>
</CollectionView.EmptyView>
</CollectionView>
</Grid>
</ContentPage.Content>
</controls:TestContentPage>

Просмотреть файл

@ -0,0 +1,94 @@
using Xamarin.Forms.Xaml;
using System;
using System.Collections.Generic;
using Xamarin.Forms.Internals;
using Xamarin.Forms.CustomAttributes;
#if UITEST
using Xamarin.UITest;
using Xamarin.UITest.Queries;
using NUnit.Framework;
using Xamarin.Forms.Core.UITests;
#endif
namespace Xamarin.Forms.Controls.Issues
{
#if UITEST
[Category(UITestCategories.CollectionView)]
#endif
#if APP
[XamlCompilation(XamlCompilationOptions.Compile)]
#endif
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 7943, "[Android] Crashes if EmptyView defined and ItemsSource is changed after ItemTemplate is changed", PlatformAffected.Android)]
public partial class Issue7943 : TestContentPage
{
public Issue7943()
{
#if APP
Device.SetFlags(new List<string> { CollectionView.CollectionViewExperimental });
Title = "Issue 7943";
InitializeComponent();
collectionView.ItemTemplate = new DataTemplate(() =>
{
var grid = new Grid();
var lbl1 = new Label();
lbl1.SetBinding(Label.TextProperty, "Name");
grid.Children.Add(lbl1);
var lbl2 = new Label();
lbl2.SetBinding(Label.TextProperty, "Age");
lbl2.SetValue(Grid.ColumnProperty, 1);
grid.Children.Add(lbl2);
return grid;
});
collectionView.ItemsSource = new List<Issue7943Model> { new Issue7943Model("John", 41), new Issue7943Model("Jane", 24) };
#endif
}
protected override void Init()
{
}
#if APP
void OnChangeTemplate(object sender, EventArgs e)
{
var random = new Random();
collectionView.ItemTemplate = new DataTemplate(() =>
{
var grid = new Grid();
grid.BackgroundColor = Color.FromRgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
var lbl1 = new Label();
lbl1.SetBinding(Label.TextProperty, "Name");
grid.Children.Add(lbl1);
return grid;
});
}
void OnChangeItemsSource(object sender, EventArgs e)
{
collectionView.ItemsSource = new List<Issue7943Model> { new Issue7943Model("Paul", 35), new Issue7943Model("Lucy", 57) };
}
void OnClearItemsSource(object sender, EventArgs e)
{
collectionView.ItemsSource = null;
}
#endif
}
[Preserve(AllMembers = true)]
class Issue7943Model
{
public string Name { get; set; }
public int Age { get; set; }
public Issue7943Model(string name, int age)
{
Name = name;
Age = age;
}
}
}

Просмотреть файл

@ -71,6 +71,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue7817.xaml.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Issue7943.xaml.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)RefreshViewTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue7338.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ScrollToGroup.cs" />
@ -1424,7 +1427,7 @@
<Compile Update="$(MSBuildThisFileDirectory)Issue7789.xaml.cs">
<DependentUpon>Issue7789.xaml</DependentUpon>
</Compile>
<Compile Update="$(MSBuildThisFileDirectory)Issue7519Xaml.xaml.cs">
<Compile Update="$(MSBuildThisFileDirectory)Issue7519Xaml.xaml.cs">
<DependentUpon>Issue7519Xaml.xaml</DependentUpon>
</Compile>
<Compile Update="$(MSBuildThisFileDirectory)Issue7817.xaml.cs">
@ -1448,6 +1451,9 @@
<Compile Update="$(MSBuildThisFileDirectory)Issue7758.xaml.cs">
<DependentUpon>Issue7758.xaml</DependentUpon>
</Compile>
<Compile Update="$(MSBuildThisFileDirectory)Issue7943.xaml.cs">
<DependentUpon>Issue7943.xaml</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue1455.xaml">
@ -1531,4 +1537,10 @@
<Generator>MSBuild:Compile</Generator>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue7943.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</EmbeddedResource>
</ItemGroup>
</Project>

Просмотреть файл

@ -28,7 +28,7 @@ namespace Xamarin.Forms.Platform.Android
public void Stop(Adapter adapter)
{
if (Observing && adapter != null)
if (Observing && adapter != null && adapter.HasObservers)
{
adapter.UnregisterAdapterDataObserver(this);
}

Просмотреть файл

@ -281,7 +281,11 @@ namespace Xamarin.Forms.Platform.Android
if (GetAdapter() != _emptyViewAdapter)
{
_emptyCollectionObserver.Stop(oldItemViewAdapter);
_itemsUpdateScrollObserver.Stop(oldItemViewAdapter);
SetAdapter(null);
SwapAdapter(ItemsViewAdapter, true);
}