[Android] CarouselView problems updating ObservableCollection (#7693) fixes #7678

* Fixed 7678: [Android] CarouselView binded to a new ObservableCollection filled with Items does not render content

* Added test instructions

* Fixed build error

* Updated sample model name
This commit is contained in:
Javier Suárez Ruiz 2019-09-30 20:07:21 +02:00 коммит произвёл Rui Marinho
Родитель 231c2e2ac7
Коммит 16504ee052
3 изменённых файлов: 148 добавлений и 4 удалений

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

@ -0,0 +1,141 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
#if UITEST
using Xamarin.Forms.Core.UITests;
using Xamarin.UITest;
using NUnit.Framework;
#endif
namespace Xamarin.Forms.Controls.Issues
{
#if UITEST
[NUnit.Framework.Category(UITestCategories.CarouselView)]
#endif
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 7678, "[Android] CarouselView binded to a new ObservableCollection filled with Items does not render content", PlatformAffected.Android)]
public class Issue7678Droid : TestContentPage
{
public Issue7678Droid()
{
Title = "Issue 7678";
BindingContext = new Issue7678DroidViewModel();
}
protected override void Init()
{
var layout = new StackLayout();
var instructions = new Label
{
Margin = new Thickness(6),
Text = "The CarouselView below must show items. If you do not see any item, this test has failed."
};
var itemsLayout =
new LinearItemsLayout(ItemsLayoutOrientation.Horizontal)
{
SnapPointsType = SnapPointsType.MandatorySingle,
SnapPointsAlignment = SnapPointsAlignment.Center
};
var carouselView = new CarouselView
{
ItemsLayout = itemsLayout,
ItemTemplate = GetCarouselTemplate()
};
carouselView.SetBinding(ItemsView.ItemsSourceProperty, "Items");
layout.Children.Add(instructions);
layout.Children.Add(carouselView);
Content = layout;
}
protected override async void OnAppearing()
{
base.OnAppearing();
await ((Issue7678DroidViewModel)BindingContext).LoadItemsAsync();
}
internal DataTemplate GetCarouselTemplate()
{
return new DataTemplate(() =>
{
var grid = new Grid();
var info = new Label
{
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
Margin = new Thickness(6)
};
info.SetBinding(Label.TextProperty, new Binding("Name"));
grid.Children.Add(info);
var frame = new Frame
{
Content = grid,
HasShadow = false
};
frame.SetBinding(BackgroundColorProperty, new Binding("Color"));
return frame;
});
}
}
[Preserve(AllMembers = true)]
public class Issue7678DroidModel
{
public Color Color { get; set; }
public string Name { get; set; }
}
[Preserve(AllMembers = true)]
public class Issue7678DroidViewModel : BindableObject
{
ObservableCollection<Issue7678DroidModel> _items;
public ObservableCollection<Issue7678DroidModel> Items
{
get { return _items; }
set
{
_items = value;
OnPropertyChanged();
}
}
public async Task LoadItemsAsync()
{
Items = new ObservableCollection<Issue7678DroidModel>();
var random = new Random();
var items = new List<Issue7678DroidModel>();
for (int n = 0; n < 5; n++)
{
items.Add(new Issue7678DroidModel
{
Color = Color.FromRgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)),
Name = $"{n + 1}"
});
}
await Task.Delay(500);
_items = new ObservableCollection<Issue7678DroidModel>(items);
OnPropertyChanged(nameof(Items));
}
}
}

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

@ -1074,6 +1074,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue7395.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue7582.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue7563.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue7678.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Bugzilla22229.xaml">
@ -1443,19 +1444,19 @@
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue7621.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue7512.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue5354.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
</Project>

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

@ -54,7 +54,9 @@ namespace Xamarin.Forms.Platform.Android
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs changedProperty)
{
if (changedProperty.Is(CarouselView.PeekAreaInsetsProperty))
if (changedProperty.Is(ItemsView.ItemsSourceProperty))
UpdateItemsSource();
else if (changedProperty.Is(CarouselView.PeekAreaInsetsProperty))
UpdatePeekAreaInsets();
else if (changedProperty.Is(CarouselView.IsSwipeEnabledProperty))
UpdateIsSwipeEnabled();