From 656835123b3a2c7257fd321fce87c5f49e6590c0 Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Tue, 3 Sep 2019 11:03:16 -0600 Subject: [PATCH] Verify sections exist before calling NumberOfItemsInSection; fixes #7338 (#7354) fixes #7338 --- .../Issue7338.cs | 71 ++++++++++++++++++++++ .../Xamarin.Forms.Controls.Issues.Shared.projitems | 3 +- .../CollectionView/GridViewLayout.cs | 6 ++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7338.cs diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7338.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7338.cs new file mode 100644 index 0000000..1f422d8 --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7338.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +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 + [Category(UITestCategories.CollectionView)] +#endif + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Github, 7338, "[Bug] CollectionView crash if source is empty in XF 4.2.0.709249", + PlatformAffected.iOS)] + class Issue7338 : TestNavigationPage + { + const string Success = "success"; + + protected override void Init() + { + FlagTestHelpers.SetCollectionViewTestFlag(); + PushAsync(CreateRoot()); + } + + Page CreateRoot() + { + var page = new ContentPage() { Title = "Issue7338" }; + + var instructions = new Label { AutomationId = Success, Text = "If you can see this label, the test has passed." }; + + var layout = new StackLayout(); + + var cv = new CollectionView + { + ItemsLayout = new GridItemsLayout(orientation: ItemsLayoutOrientation.Horizontal), + ItemTemplate = new DataTemplate(() => + { + return Template(); + }) + }; + + layout.Children.Add(instructions); + layout.Children.Add(cv); + + page.Content = layout; + + return page; + } + + View Template() + { + var label1 = new Label { Text = "Text", HeightRequest = 100 }; + return label1; + } + +#if UITEST + [Test] + public void EmptyHorizontalCollectionShouldNotCrash() + { + // If the instructions are visible at all, then this has succeeded + RunningApp.WaitForElement(Success); + } +#endif + } +} diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index f12c343..a466d0e 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -32,6 +32,7 @@ + @@ -1371,4 +1372,4 @@ MSBuild:UpdateDesignTimeXaml - + \ No newline at end of file diff --git a/Xamarin.Forms.Platform.iOS/CollectionView/GridViewLayout.cs b/Xamarin.Forms.Platform.iOS/CollectionView/GridViewLayout.cs index f8e52e1..b1f07a9 100644 --- a/Xamarin.Forms.Platform.iOS/CollectionView/GridViewLayout.cs +++ b/Xamarin.Forms.Platform.iOS/CollectionView/GridViewLayout.cs @@ -138,6 +138,12 @@ namespace Xamarin.Forms.Platform.iOS return false; } + if (CollectionView.NumberOfSections() == 0) + { + // And it only happens if there are items + return false; + } + if (EstimatedItemSize.IsEmpty) { // The bug only occurs when using Autolayout; with a set ItemSize, we don't have to worry about it -- 2.7.4