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

Commit

Permalink
Fix UWP issue with invisible CollectionView and layouts (#14593)
Browse files Browse the repository at this point in the history
Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>
  • Loading branch information
jsuarezruiz and jfversluis committed Oct 11, 2021
1 parent 3ea43c5 commit 197145c
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8" ?>
<controls:TestContentPage
xmlns:controls="clr-namespace:Xamarin.Forms.Controls"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Xamarin.Forms.Controls.Issues.Issue9079"
Title="Issue 9079">
<ContentPage.Content>
<StackLayout>
<Button
Text="Show/Hide CollectionView"
Clicked="Button_Clicked" />
<CollectionView
x:Name="collectionView"
IsVisible="False">
<CollectionView.ItemsLayout>
<GridItemsLayout
Orientation="Vertical"
Span="3" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Label
VerticalTextAlignment="Center"
Text="{Binding}" />
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
</ContentPage.Content>
</controls:TestContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Diagnostics;
using System.Collections.Generic;
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
{
#if UITEST
[Category(UITestCategories.CollectionView)]
#endif
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 9079,
"[Bug] [UWP] CollectionView ItemsLayout not working when initially invisible",
PlatformAffected.UWP)]
public partial class Issue9079 : TestContentPage
{
public Issue9079()
{
#if APP
InitializeComponent();
GenerateItems();
#endif
}

protected override void Init()
{

}
#if APP
void GenerateItems()
{
List<string> items = new List<string>();
for (int i = 0; i < 100; i++)
items.Add($"Hello world {i}!");

collectionView.ItemsSource = items;
}

void Button_Clicked(object sender, EventArgs e)
{
collectionView.IsVisible = !collectionView.IsVisible;
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1782,6 +1782,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue11795.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue14528.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue14286.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue9079.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13588.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue10101.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13818.xaml.cs" />
Expand Down Expand Up @@ -2243,6 +2244,9 @@
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue14286.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue9079.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue13588.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE
{
UpdateFooter();
}
else if (changedProperty.Is(StructuredItemsView.ItemsLayoutProperty))
else if (changedProperty.IsOneOf(VisualElement.IsVisibleProperty, StructuredItemsView.ItemsLayoutProperty))
{
UpdateItemsLayout();
}
Expand Down

1 comment on commit 197145c

@adrian-sal-kennedy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't appear to have solved the issue definitively - in roughly 1/3 of app loads for me I still get an empty view.

What it did do was break my appallingly bad workaround (which was to toggle IsVisible at a short interval until the element had received a Width that was not -1). No matter, I made it InvalidateMeasure() instead of IsVisible and now the workaround works again, but the CollectionView issue remains.

It seems to be a race condition - it only happens on running the project, not on loading a page as such, and from what I've gathered the times it fails seem to be when the program takes longer to load (if I set my hacky workaround to count how many times it triggers itself and waits, I can get a rough time when it finally works - normally around 200ms, sometimes as much as 800-1200ms).

Anyway I'm a bit of a Xamarin noob but I thought it may be worth making a comment here.

Please sign in to comment.