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

Commit

Permalink
Invalidate CollectionView Layout on transition to IsVisible = true (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
hartez authored Jan 15, 2021
1 parent 626f756 commit c419953
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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, 13203, "[Bug] [iOS] CollectionView does not bind to items if `IsVisible=False`", PlatformAffected.iOS)]
#if UITEST
[NUnit.Framework.Category(UITestCategories.CollectionView)]
#endif
public class Issue13203 : TestContentPage
{
const string Success = "Success";

protected override void Init()
{
var cv = new CollectionView
{
IsVisible = false,

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

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

Appearing += (sender, args) => { cv.IsVisible = true; };
}

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

#if UITEST
[Test]
public void SettingGroupedCollectionViewItemSourceNullShouldNotCrash()
{
RunningApp.WaitForElement(Success);
}
#endif
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Issue12246.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue8613.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13203.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue9137.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue8691.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue7606.cs" />
Expand Down
16 changes: 16 additions & 0 deletions Xamarin.Forms.Platform.iOS/CollectionView/ItemsViewController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using CoreGraphics;
using Foundation;
using UIKit;
Expand Down Expand Up @@ -27,6 +28,8 @@ protected ItemsViewController(TItemsView itemsView, ItemsViewLayout layout) : ba
{
ItemsView = itemsView;
ItemsViewLayout = layout;

ItemsView.PropertyChanged += ItemsViewPropertyChanged;
}

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

if (disposing)
{
ItemsView.PropertyChanged -= ItemsViewPropertyChanged;

ItemsSource?.Dispose();
CollectionView.Delegate = null;
Delegator?.Dispose();
Expand Down Expand Up @@ -457,5 +462,16 @@ public TemplatedCell CreateMeasurementCell(NSIndexPath indexPath)

return templatedCell;
}

void ItemsViewPropertyChanged(object sender, PropertyChangedEventArgs changedProperty)
{
if (changedProperty.Is(VisualElement.IsVisibleProperty))
{
if (ItemsView.IsVisible)
{
Layout.InvalidateLayout();
}
}
}
}
}

0 comments on commit c419953

Please sign in to comment.