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

Commit 85f7e4f

Browse files
jsuarezruizhartez
andauthored
Notify child added to CollectionView (#10812) fixes #10454
* Notify child added to CollectionView * Automate the test * Fixed merge problem Co-authored-by: E.Z. Hart <[email protected]>
1 parent 5eb6e1e commit 85f7e4f

File tree

3 files changed

+110
-3
lines changed

3 files changed

+110
-3
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System;
2+
using System.Collections.ObjectModel;
3+
using Xamarin.Forms.CustomAttributes;
4+
using Xamarin.Forms.Internals;
5+
6+
#if UITEST
7+
using Xamarin.Forms.Core.UITests;
8+
using Xamarin.UITest;
9+
using NUnit.Framework;
10+
#endif
11+
12+
namespace Xamarin.Forms.Controls.Issues
13+
{
14+
#if UITEST
15+
[Category(UITestCategories.CollectionView)]
16+
#endif
17+
[Preserve(AllMembers = true)]
18+
[Issue(IssueTracker.Github, 10454, "CollectionView ChildAdded", PlatformAffected.All)]
19+
public class Issue10454 : TestContentPage
20+
{
21+
const string Success = "Success";
22+
23+
protected override void Init()
24+
{
25+
Title = "Issue 10454";
26+
27+
BindingContext = new Issue10454ViewModel();
28+
29+
var layout = new StackLayout();
30+
31+
var collectionView = new CollectionView();
32+
collectionView.SetBinding(ItemsView.ItemsSourceProperty, "Items");
33+
34+
collectionView.ItemTemplate = new DataTemplate(() =>
35+
{
36+
var template = new DataTemplate();
37+
var content = new Grid
38+
{
39+
BackgroundColor = Color.LightGray
40+
};
41+
var label = new Label();
42+
label.SetBinding(Label.TextProperty, ".");
43+
content.Children.Add(label);
44+
45+
return content;
46+
});
47+
48+
var labelInfo = new Label
49+
{
50+
FontSize = 18
51+
};
52+
53+
var successLabel = new Label();
54+
55+
layout.Children.Add(labelInfo);
56+
layout.Children.Add(successLabel);
57+
layout.Children.Add(collectionView);
58+
59+
Content = layout;
60+
61+
collectionView.ChildAdded += (sender, args) =>
62+
{
63+
labelInfo.Text = $"ChildAdded {args.Element}";
64+
Console.WriteLine(labelInfo.Text);
65+
66+
successLabel.Text = Success;
67+
};
68+
69+
collectionView.ChildRemoved += (sender, args) =>
70+
{
71+
labelInfo.Text = $"ChildRemoved {args.Element}";
72+
Console.WriteLine(labelInfo.Text);
73+
};
74+
}
75+
76+
#if UITEST
77+
[Test]
78+
public void ChildAddedShouldFire()
79+
{
80+
RunningApp.WaitForElement(Success);
81+
}
82+
#endif
83+
}
84+
85+
[Preserve(AllMembers = true)]
86+
public class Issue10454ViewModel : BindableObject
87+
{
88+
public Issue10454ViewModel()
89+
{
90+
LoadItems();
91+
}
92+
93+
public ObservableCollection<string> Items { get; set; }
94+
95+
void LoadItems()
96+
{
97+
Items = new ObservableCollection<string>();
98+
99+
for (int i = 0; i < 100; i++)
100+
{
101+
Items.Add($"Item {i+1}");
102+
}
103+
}
104+
}
105+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,7 @@
14991499
<Compile Include="$(MSBuildThisFileDirectory)Issue11723.cs" />
15001500
<Compile Include="$(MSBuildThisFileDirectory)Issue11496.xaml.cs" />
15011501
<Compile Include="$(MSBuildThisFileDirectory)Issue11209.xaml.cs" />
1502+
<Compile Include="$(MSBuildThisFileDirectory)Issue10454.cs" />
15021503
<Compile Include="$(MSBuildThisFileDirectory)Issue11081.xaml.cs" />
15031504
</ItemGroup>
15041505
<ItemGroup>
@@ -2299,4 +2300,4 @@
22992300
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
23002301
</EmbeddedResource>
23012302
</ItemGroup>
2302-
</Project>
2303+
</Project>

Xamarin.Forms.Core/Items/ItemsView.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,8 @@ public void AddLogicalChild(Element element)
9797
}
9898

9999
_logicalChildren.Add(element);
100-
101100
element.Parent = this;
102-
101+
OnChildAdded(element);
103102
VisualDiagnostics.OnChildAdded(this, element);
104103
}
105104

@@ -111,11 +110,13 @@ public void RemoveLogicalChild(Element element)
111110
}
112111

113112
element.Parent = null;
113+
114114
if (!_logicalChildren.Contains(element))
115115
return;
116116

117117
var oldLogicalIndex = _logicalChildren.IndexOf(element);
118118
_logicalChildren.Remove(element);
119+
OnChildRemoved(element, oldLogicalIndex);
119120
VisualDiagnostics.OnChildRemoved(this, element, oldLogicalIndex);
120121
}
121122

0 commit comments

Comments
 (0)