Skip to content

Commit

Permalink
Revert changes to setting context and add tests (#17348)
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen authored Sep 14, 2023
1 parent aad88d6 commit 1825d26
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
48 changes: 48 additions & 0 deletions src/Controls/samples/Controls.Sample.UITests/Issues/Issue16787.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Microsoft.Maui.Controls;

namespace Maui.Controls.Sample.Issues
{
[Issue(IssueTracker.Github, 16787, "CollectionView runtime binding errors when loading the ItemSource asynchronously", PlatformAffected.UWP)]
public class Issue16787 : TestContentPage
{
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
}
protected override void Init()
{
var cv = new CollectionView();
cv.BindingContextChanged += Cv_BindingContextChanged;
this.BindingContext = this;
cv.ItemTemplate = new DataTemplate(() =>
{
int bindingContextChanges = 0;
var label = new Label();
label.BindingContextChanged += (_, _) =>
{
bindingContextChanges++;
label.Text = bindingContextChanges.ToString();
};
label.AutomationId = "LabelBindingCount";
return label;
});

cv.ItemsSource = new[] { "random" };


var layout = new VerticalStackLayout()
{
new Label()
{
Text = "The value below this label should be a 1. That's how many times the BindingContext has changed on the Templated element"
}
};

Content = cv;
}

private void Cv_BindingContextChanged(object sender, System.EventArgs e)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ internal void Realize()
// or if we need to switch DataTemplates (because this instance is being recycled)
// then we'll need to create the content from the template
_visualElement = formsTemplate.CreateContent(dataContext, container) as VisualElement;
_visualElement.BindingContext = dataContext;
_renderer = _visualElement.ToHandler(mauiContext);

// We need to set IsPlatformStateConsistent explicitly; otherwise, it won't be set until the renderer's Loaded
Expand All @@ -186,11 +187,11 @@ internal void Realize()
{
// We are reusing this ItemContentControl and we can reuse the Element
_visualElement = _renderer.VirtualView as VisualElement;
_visualElement.BindingContext = dataContext;
}

Content = _renderer.ToPlatform();
itemsView?.AddLogicalChild(_visualElement);
_visualElement.BindingContext = dataContext;
}

void SetNativeStateConsistent(VisualElement visualElement)
Expand Down
20 changes: 20 additions & 0 deletions src/Controls/tests/UITests/Tests/Issues/Issue16787.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.Maui.Appium;
using NUnit.Framework;

namespace Microsoft.Maui.AppiumTests.Issues
{
public class Issue16787 : _IssuesUITest
{
public Issue16787(TestDevice device) : base(device)
{
}

public override string Issue => "CollectionView runtime binding errors when loading the ItemSource asynchronously";

[Test]
public void CollectionViewBindingContextOnlyChangesOnce()
{
Assert.AreEqual("1", App.WaitForElement("LabelBindingCount")[0].ReadText());
}
}
}

0 comments on commit 1825d26

Please sign in to comment.