Skip to content

Commit e5d819d

Browse files
kubaflormarinho
authored andcommitted
[CollectionView] Crash occurs when switching CollectionView.IsVisible right after setting ItemsSource (#28194)
* Update PropertyPropagationExtensions.cs * Update PropertyPropagationExtensions.cs * Added a UI Test * Performance improvement * Update PropertyPropagationExtensions.cs * Update PropertyPropagationExtensions.cs * Update PropertyPropagationExtensions.cs * Use foreach * Use to array
1 parent 150db6e commit e5d819d

File tree

4 files changed

+82
-2
lines changed

4 files changed

+82
-2
lines changed

src/Controls/src/Core/Internals/PropertyPropagationExtensions.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#nullable disable
22
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
35

46
namespace Microsoft.Maui.Controls.Internals
57
{
68
/// <include file="../../../docs/Microsoft.Maui.Controls.Internals/PropertyPropagationExtensions.xml" path="Type[@FullName='Microsoft.Maui.Controls.Internals.PropertyPropagationExtensions']/Docs/*" />
79
public static class PropertyPropagationExtensions
810
{
9-
internal static void PropagatePropertyChanged(string propertyName, Element element, IEnumerable children)
11+
internal static void PropagatePropertyChanged(string propertyName, Element element, IReadOnlyList<IVisualTreeElement> children)
1012
{
1113
if (propertyName == null || propertyName == VisualElement.FlowDirectionProperty.PropertyName)
1214
SetFlowDirectionFromParent(element);
@@ -26,10 +28,12 @@ internal static void PropagatePropertyChanged(string propertyName, Element eleme
2628
if (propertyName == null || propertyName == Shell.NavBarIsVisibleProperty.PropertyName)
2729
BaseShellItem.PropagateFromParent(Shell.NavBarIsVisibleProperty, element);
2830

29-
foreach (var child in children)
31+
foreach (var child in children.ToArray())
3032
{
3133
if (child is IPropertyPropagationController view)
34+
{
3235
view.PropagatePropertyChanged(propertyName);
36+
}
3337
}
3438
}
3539

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
x:Class="Maui.Controls.Sample.Issues.Issue28162">
5+
<VerticalStackLayout>
6+
<Button Text="Toggle visibility"
7+
AutomationId="button"
8+
Clicked="Button_Clicked"/>
9+
<CollectionView IsVisible="True"
10+
HorizontalOptions="Center"
11+
VerticalOptions="Center"
12+
x:Name="CollectionView">
13+
<CollectionView.Header>
14+
<ContentView HeightRequest="50"
15+
BackgroundColor="Green"/>
16+
</CollectionView.Header>
17+
<CollectionView.ItemTemplate>
18+
<DataTemplate>
19+
<Border StrokeShape="RoundRectangle 10"
20+
Padding="20"
21+
Stroke="Red">
22+
<Label HorizontalOptions="Center"
23+
VerticalOptions="Center"
24+
Text="{Binding .}"/>
25+
</Border>
26+
</DataTemplate>
27+
</CollectionView.ItemTemplate>
28+
<CollectionView.Footer>
29+
<ContentView HeightRequest="50"
30+
BackgroundColor="Green"/>
31+
</CollectionView.Footer>
32+
</CollectionView>
33+
</VerticalStackLayout>
34+
</ContentPage>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Collections.ObjectModel;
2+
3+
namespace Maui.Controls.Sample.Issues
4+
{
5+
[Issue(IssueTracker.Github, 28162, "Crash occurs when switching CollectionView.IsVisible right after setting ItemsSource", PlatformAffected.iOS)]
6+
public partial class Issue28162 : ContentPage
7+
{
8+
public Issue28162()
9+
{
10+
InitializeComponent();
11+
}
12+
13+
private void Button_Clicked(object sender, EventArgs e)
14+
{
15+
CollectionView.ItemsSource = new ObservableCollection<string> { "Item 1", "Item 2", "Item 3" };
16+
CollectionView.IsVisible = !CollectionView.IsVisible;
17+
}
18+
}
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues
6+
{
7+
public class Issue28162 : _IssuesUITest
8+
{
9+
public Issue28162(TestDevice testDevice) : base(testDevice)
10+
{
11+
}
12+
13+
public override string Issue => "Crash occurs when switching CollectionView.IsVisible right after setting ItemsSource";
14+
15+
[Test]
16+
[Category(UITestCategories.CollectionView)]
17+
public void SwitchingVisibilityAndChangingItemsSourceShouldNotCrash()
18+
{
19+
App.WaitForElement("button");
20+
App.Click("button");
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)