diff --git a/src/Core/src/Core/Extensions/VisualTreeElementExtensions.cs b/src/Core/src/Core/Extensions/VisualTreeElementExtensions.cs index dcd56aa2d1cb..ad6d9b6c1532 100644 --- a/src/Core/src/Core/Extensions/VisualTreeElementExtensions.cs +++ b/src/Core/src/Core/Extensions/VisualTreeElementExtensions.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using Microsoft.Maui.Graphics; @@ -145,11 +145,13 @@ static List GetVisualTreeElementsWindowsInternal(IVisualTree if (uiElement != null) { - var uniqueElements = findChildren(uiElement).Distinct(); - var viewTree = visualElement.GetVisualTreeDescendants().Where(n => n is IView view && view.Handler is not null).Select(n => new Tuple((IView)n, ((IView)n).ToPlatform())); - var testList = viewTree.Where(n => uniqueElements.Contains(n.Item2)).Select(n => n.Item1); - if (testList != null && testList.Any()) - visualElements.AddRange(testList.Select(n => (IVisualTreeElement)n)); + var uniqueElements = findChildren(uiElement).ToHashSet(); + + var testList = visualElement.GetVisualTreeDescendants() + .Where(n => n is IView view && view.Handler is not null && uniqueElements.Contains(view.ToPlatform())); + + if (testList.Any()) + visualElements.AddRange(testList); } visualElements.Reverse();