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

Commit c419953

Browse files
authored
Invalidate CollectionView Layout on transition to IsVisible = true (#13370) fixes #13203
* Automated test * Tell CollectionView Layout to invalidate if IsVisible transitions to true; fixes #13203 * Remove old waitforelement call * Unsubscribe property changed handler during Dispose
1 parent 626f756 commit c419953

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.Text;
5+
using Xamarin.Forms.CustomAttributes;
6+
using Xamarin.Forms.Internals;
7+
8+
#if UITEST
9+
using Xamarin.UITest;
10+
using NUnit.Framework;
11+
using Xamarin.Forms.Core.UITests;
12+
#endif
13+
14+
namespace Xamarin.Forms.Controls.Issues
15+
{
16+
[Issue(IssueTracker.Github, 13203, "[Bug] [iOS] CollectionView does not bind to items if `IsVisible=False`", PlatformAffected.iOS)]
17+
#if UITEST
18+
[NUnit.Framework.Category(UITestCategories.CollectionView)]
19+
#endif
20+
public class Issue13203 : TestContentPage
21+
{
22+
const string Success = "Success";
23+
24+
protected override void Init()
25+
{
26+
var cv = new CollectionView
27+
{
28+
IsVisible = false,
29+
30+
ItemTemplate = new DataTemplate(() =>
31+
{
32+
var label = new Label();
33+
label.SetBinding(Label.TextProperty, new Binding(nameof(Item.Text)));
34+
return label;
35+
})
36+
};
37+
38+
var source = new List<Item> { new Item { Text = Success } };
39+
cv.ItemsSource = source;
40+
Content = cv;
41+
42+
Appearing += (sender, args) => { cv.IsVisible = true; };
43+
}
44+
45+
class Item
46+
{
47+
public string Text { get; set; }
48+
}
49+
50+
#if UITEST
51+
[Test]
52+
public void SettingGroupedCollectionViewItemSourceNullShouldNotCrash()
53+
{
54+
RunningApp.WaitForElement(Success);
55+
}
56+
#endif
57+
}
58+
}
59+

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
</Compile>
3939
<Compile Include="$(MSBuildThisFileDirectory)Issue12246.cs" />
4040
<Compile Include="$(MSBuildThisFileDirectory)Issue8613.cs" />
41+
<Compile Include="$(MSBuildThisFileDirectory)Issue13203.cs" />
4142
<Compile Include="$(MSBuildThisFileDirectory)Issue9137.cs" />
4243
<Compile Include="$(MSBuildThisFileDirectory)Issue8691.cs" />
4344
<Compile Include="$(MSBuildThisFileDirectory)Issue7606.cs" />

Xamarin.Forms.Platform.iOS/CollectionView/ItemsViewController.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.ComponentModel;
23
using CoreGraphics;
34
using Foundation;
45
using UIKit;
@@ -27,6 +28,8 @@ protected ItemsViewController(TItemsView itemsView, ItemsViewLayout layout) : ba
2728
{
2829
ItemsView = itemsView;
2930
ItemsViewLayout = layout;
31+
32+
ItemsView.PropertyChanged += ItemsViewPropertyChanged;
3033
}
3134

3235
public void UpdateLayout(ItemsViewLayout newLayout)
@@ -56,6 +59,8 @@ protected override void Dispose(bool disposing)
5659

5760
if (disposing)
5861
{
62+
ItemsView.PropertyChanged -= ItemsViewPropertyChanged;
63+
5964
ItemsSource?.Dispose();
6065
CollectionView.Delegate = null;
6166
Delegator?.Dispose();
@@ -457,5 +462,16 @@ public TemplatedCell CreateMeasurementCell(NSIndexPath indexPath)
457462

458463
return templatedCell;
459464
}
465+
466+
void ItemsViewPropertyChanged(object sender, PropertyChangedEventArgs changedProperty)
467+
{
468+
if (changedProperty.Is(VisualElement.IsVisibleProperty))
469+
{
470+
if (ItemsView.IsVisible)
471+
{
472+
Layout.InvalidateLayout();
473+
}
474+
}
475+
}
460476
}
461477
}

0 commit comments

Comments
 (0)