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

Commit

Permalink
Notify child added to CollectionView (#10812) fixes #10454
Browse files Browse the repository at this point in the history
* Notify child added to CollectionView

* Automate the test

* Fixed merge problem

Co-authored-by: E.Z. Hart <hartez@gmail.com>
  • Loading branch information
jsuarezruiz and hartez authored Sep 18, 2020
1 parent 5eb6e1e commit 85f7e4f
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System;
using System.Collections.ObjectModel;
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, 10454, "CollectionView ChildAdded", PlatformAffected.All)]
public class Issue10454 : TestContentPage
{
const string Success = "Success";

protected override void Init()
{
Title = "Issue 10454";

BindingContext = new Issue10454ViewModel();

var layout = new StackLayout();

var collectionView = new CollectionView();
collectionView.SetBinding(ItemsView.ItemsSourceProperty, "Items");

collectionView.ItemTemplate = new DataTemplate(() =>
{
var template = new DataTemplate();
var content = new Grid
{
BackgroundColor = Color.LightGray
};
var label = new Label();
label.SetBinding(Label.TextProperty, ".");
content.Children.Add(label);
return content;
});

var labelInfo = new Label
{
FontSize = 18
};

var successLabel = new Label();

layout.Children.Add(labelInfo);
layout.Children.Add(successLabel);
layout.Children.Add(collectionView);

Content = layout;

collectionView.ChildAdded += (sender, args) =>
{
labelInfo.Text = $"ChildAdded {args.Element}";
Console.WriteLine(labelInfo.Text);
successLabel.Text = Success;
};

collectionView.ChildRemoved += (sender, args) =>
{
labelInfo.Text = $"ChildRemoved {args.Element}";
Console.WriteLine(labelInfo.Text);
};
}

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

[Preserve(AllMembers = true)]
public class Issue10454ViewModel : BindableObject
{
public Issue10454ViewModel()
{
LoadItems();
}

public ObservableCollection<string> Items { get; set; }

void LoadItems()
{
Items = new ObservableCollection<string>();

for (int i = 0; i < 100; i++)
{
Items.Add($"Item {i+1}");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue11723.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11496.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11209.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue10454.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11081.xaml.cs" />
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -2299,4 +2300,4 @@
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
</Project>
</Project>
5 changes: 3 additions & 2 deletions Xamarin.Forms.Core/Items/ItemsView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ public void AddLogicalChild(Element element)
}

_logicalChildren.Add(element);

element.Parent = this;

OnChildAdded(element);
VisualDiagnostics.OnChildAdded(this, element);
}

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

element.Parent = null;

if (!_logicalChildren.Contains(element))
return;

var oldLogicalIndex = _logicalChildren.IndexOf(element);
_logicalChildren.Remove(element);
OnChildRemoved(element, oldLogicalIndex);
VisualDiagnostics.OnChildRemoved(this, element, oldLogicalIndex);
}

Expand Down

0 comments on commit 85f7e4f

Please sign in to comment.