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

Commit

Permalink
Signal layout request when CollectionView is in a layout
Browse files Browse the repository at this point in the history
Fixes #13551
  • Loading branch information
hartez committed Feb 1, 2021
1 parent cf7d0d4 commit 2abf85e
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ protected override void Init()

var source = new List<Item> { new Item { Text = Success } };
cv.ItemsSource = source;

Content = cv;

Appearing += (sender, args) => { cv.IsVisible = true; };
Expand All @@ -49,7 +50,7 @@ class Item

#if UITEST
[Test]
public void SettingGroupedCollectionViewItemSourceNullShouldNotCrash()
public void CollectionShouldInvalidateOnVisibilityChange()
{
RunningApp.WaitForElement(Success);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Text;
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
{
[Issue(IssueTracker.Github, 13551, "[Bug] [iOS] CollectionView does not display items if `IsVisible` modified via a binding/trigger", PlatformAffected.iOS)]
#if UITEST
[NUnit.Framework.Category(UITestCategories.CollectionView)]
#endif
public class Issue13551 : TestContentPage
{
const string Success1 = "Success1";
const string Success2 = "Success2";

public ObservableCollection<Item> Source1 { get; } = new ObservableCollection<Item>();
public ObservableCollection<Item> Source2 { get; } = new ObservableCollection<Item>();

CollectionView BindingWithConverter()
{
var cv = new CollectionView
{
IsVisible = true,

ItemTemplate = new DataTemplate(() =>
{
var label = new Label();
label.SetBinding(Label.TextProperty, new Binding(nameof(Item.Text)));
return label;
})
};

cv.SetBinding(CollectionView.ItemsSourceProperty, new Binding("Source1"));
cv.SetBinding(VisualElement.IsVisibleProperty, new Binding("Source1.Count", converter: new IntToBoolConverter()));

return cv;
}

CollectionView WithTrigger()
{
var cv = new CollectionView
{
IsVisible = true,

ItemTemplate = new DataTemplate(() =>
{
var label = new Label();
label.SetBinding(Label.TextProperty, new Binding(nameof(Item.Text)));
return label;
})
};

cv.SetBinding(CollectionView.ItemsSourceProperty, new Binding("Source2"));

var trigger = new DataTrigger(typeof(CollectionView));
trigger.Value = 0;
trigger.Setters.Add(new Setter() { Property = VisualElement.IsVisibleProperty, Value = false });
trigger.Binding = new Binding("Source2.Count");

cv.Triggers.Add(trigger);

return cv;
}

protected override void Init()
{
BindingContext = this;

var cv1 = BindingWithConverter();
var cv2 = WithTrigger();

var grid = new Grid
{
RowDefinitions = new RowDefinitionCollection
{
new RowDefinition() { Height = GridLength.Star },
new RowDefinition() { Height = GridLength.Star },
}
};

grid.Children.Add(cv1);
grid.Children.Add(cv2);
Grid.SetRow(cv2, 1);

Content = grid;

Device.StartTimer(TimeSpan.FromMilliseconds(300), () =>
{
Device.BeginInvokeOnMainThread(() =>
{
Source1.Add(new Item { Text = Success1 });
Source2.Add(new Item { Text = Success2 });
});

return false;
});
}

class IntToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value is int val && val > 0;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

public class Item
{
public string Text { get; set; }
}

#if UITEST
[Test]
public void CollectionInLayoutShouldInvalidateOnVisibilityChange()
{
RunningApp.WaitForElement(Success1);
RunningApp.WaitForElement(Success2);
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<Compile Include="$(MSBuildThisFileDirectory)CollectionViewGroupTypeIssue.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11214.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13109.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13551.cs" />
<Compile Include="$(MSBuildThisFileDirectory)RadioButtonTemplateFromStyle.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ShellWithCustomRendererDisabledAnimations.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ShellFlyoutContent.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ void ItemsViewPropertyChanged(object sender, PropertyChangedEventArgs changedPro
if (ItemsView.IsVisible)
{
Layout.InvalidateLayout();
CollectionView.LayoutIfNeeded();
}
}
}
Expand Down

0 comments on commit 2abf85e

Please sign in to comment.