You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: Some work was previously started for this in #16177. The PR has additional scenario to test.
Unfortunately it looks like I forgot to add a comment on the PR on why I was blocked on continuing it, and it's a bit late for me to memorize. I very well recall though that matching WinUI exactly wasn't trivial, and there was some "Enter" phase that is initiated early, probably requiring changes to Xaml generator.
One of the things that are required for proper FindName implementation is also NameScope (it was probably this part that require changes in Xaml generator?). Also note that there were weird behaviors when elements are removed from the visual tree. I think I have few tests for such scenarios in my PR (hopefully covering most cases).
In general, depending on the level of correctness needed here, things can get very complicated. Especially if you will only care about Skia now, some conditional code will be needed in Xaml generator, most likely, to support NameScope properly on Skia-only. Lots of stuff are needing proper lifecycle and are almost impossible to implement correctly on native platforms without fixing lifecycle first.
I hope my PR can be a good starting point for this.
This issue has had no activity for 120 days. It will be closed in 10 days if there is no further activity. Remove the Stale label or leave a comment to keep it open.
Adding a concrete, minimal repro for one class of the divergence: FindName never returns named objects that are not FrameworkElements, such as Run, Hyperlink, or a brush in Resources.
The same happens with plain Name= instead of x:Name=, and when the TextBlock is loaded into the live visual tree before calling FindName.
Evidence
Uno, runtime-tested on Skia Desktop at master bfc8dff: FindName returns null in all 6 cases (Run, Hyperlink with x:Name and Name, brush in Grid.Resources, Run in a loaded tree). A control case, <Border x:Name> inside a Grid, passes.
WinUI, runtime-tested in a small unpackaged WinAppSDK app with the same XamlReader.Load snippets: all of them return the named object (Run, Hyperlink, SolidColorBrush), with both x:Name and Name.
WinUI source:FrameworkElement::FindNameImpl is a namescope table lookup (CCoreServices::TryGetElementByName), so any DependencyObject the parser registered by name is found. It does not walk the visual tree.
Uno root cause:IFrameworkElementHelper.FindName walks visual children and matches IFrameworkElement.Name only, with special cases for ContentControl/UserControl content, Popup child and flyouts. It never consults a NameScope, so TextElements and resources can't be found. The runtime XamlReader path also registers no names in any NameScope.
Workaround: use the generated field (compiled XAML), or walk TextBlock.Inlines / Resources manually.
Two more concrete divergences under this umbrella, both reproducing at master cc485416010 (Skia Desktop runtime tests, repros below). Both come from the same root cause: Uno's FindName walks the live visual tree, while WinUI resolves names through the XAML namescope.
WinUI behavior (C++ sources):FrameworkElement::FindNameImpl calls CCoreServices::TryGetElementByName(name, GetHandle()) (FrameworkElement_partial.cpp). That resolves the reference object's namescope owner via CDependencyObject::GetStandardNameScopeOwnerInternal (depends.cpp) and looks the name up in that owner's table (xcpcore_namescope.cpp). It never looks at where the element currently sits in the visual tree. Not run on native WinUI, this is from reading the sources.
Uno:IFrameworkElementHelper.FindName matches direct children's names first, then recurses into children last-to-first. There is no namescope boundary.
1. A nested UserControl's x:Name is returned (and preferred)
A UserControl declares x:Name="Target" (inside a StackPanel) plus a ContentControl named Host, after the panel. An instance of the same control is hosted as outer.Host.Content.
Result on Skia Desktop: Assert.AreNotSame(inner.Target, outer.FindName("Target")) fails, so FindName returned the nested control's element, because it sits in the last child, which is searched first. In an app this shows up as silently wrong geometry or state rather than null.
2. CommandBar commands in the closed overflow are not found
Hosted in a UserControl with Width = 200. After layout, Bar.PrimaryCommands.OfType<AppBarButton>().Where(b => b.IsInOverflow) is non-empty, and for those buttons root.FindName(button.Name) returns null. The first overflowed button reported is Third. An overflowed command has no visual parent while the overflow popup is closed, so the tree walk never reaches it. In WinUI the command stays registered in the namescope, so it is found.
Full test class (two UserControls plus Given_FindNameScope), ready to drop into Uno.UI.RuntimeTests:
Given_FindNameScope.cs
[TestClass][RunsOnUIThread]publicclassGiven_FindNameScope{[TestMethod]publicasyncTaskWhen_Nested_UserControl_Declares_Same_Name_Then_Outer_Own_Element_Is_Returned(){FindNameNestedProbeouter=new();FindNameNestedProbeinner=new();outer.Host.Content=inner;TestServices.WindowHelper.WindowContent=outer;awaitTestServices.WindowHelper.WaitForLoaded(outer);Assert.AreNotSame(inner.Target,outer.FindName("Target"));Assert.AreSame(outer.Target,outer.FindName("Target"));}[TestMethod]publicasyncTaskWhen_CommandBar_Command_Is_In_Closed_Overflow_Then_FindName_Finds_It(){FindNameOverflowProberoot=new(){Width=200};TestServices.WindowHelper.WindowContent=root;awaitTestServices.WindowHelper.WaitForLoaded(root);awaitTestServices.WindowHelper.WaitForIdle();varoverflowed=root.Bar.PrimaryCommands.OfType<AppBarButton>().Where(b =>b.IsInOverflow).ToList();Assert.IsTrue(overflowed.Count>0);foreach(varbuttoninoverflowed){Assert.AreSame(button,root.FindName(button.Name),$"{button.Name} is in the closed overflow");}}}
FindNameNestedProbe is <Grid><StackPanel><TextBlock x:Name="Target" x:FieldModifier="public"/></StackPanel><ContentControl x:Name="Host" x:FieldModifier="public"/></Grid>; FindNameOverflowProbe is the CommandBar above, both with x:FieldModifier="public" on the names.
Both fail on master today; both should pass once names resolve per namescope owner, so they would make good regression tests for the namescope work.
Workaround: use the generated x:Name field instead of FindName across nesting, and look commands up in CommandBar.PrimaryCommands/SecondaryCommands by Name.
Activity
FrameworkElement.FindNamedoesn't look for resources #7043 as a duplicate of this issueFindNamedoes not work on elements that are not in visual tree #17020 as a duplicate of this issueMartinZikmund commentedon Jul 23, 2025
When fixing, ensure the scenarios in the linked duplicate issues are also working correctly
VisualTreeHelper.GetParentin WinUI ensures the parent is UIElement #14670VisualTreeHelper.GetParentin WinUI ensures the parent is UIElement #14670Youssef1313 commentedon Jul 25, 2025
Note: Some work was previously started for this in #16177. The PR has additional scenario to test.
Unfortunately it looks like I forgot to add a comment on the PR on why I was blocked on continuing it, and it's a bit late for me to memorize. I very well recall though that matching WinUI exactly wasn't trivial, and there was some "Enter" phase that is initiated early, probably requiring changes to Xaml generator.
One of the things that are required for proper
FindNameimplementation is alsoNameScope(it was probably this part that require changes in Xaml generator?). Also note that there were weird behaviors when elements are removed from the visual tree. I think I have few tests for such scenarios in my PR (hopefully covering most cases).In general, depending on the level of correctness needed here, things can get very complicated. Especially if you will only care about Skia now, some conditional code will be needed in Xaml generator, most likely, to support
NameScopeproperly on Skia-only. Lots of stuff are needing proper lifecycle and are almost impossible to implement correctly on native platforms without fixing lifecycle first.I hope my PR can be a good starting point for this.
MartinZikmund commentedon Jan 22, 2026
Another very dangerous example:
Notice how the "BackButton" is completely outside the
ContentFrame. However, whenOnApplyTemplateofFrameruns, it calls:Which ends up returning the button that is outside the frame!
github-actions commentedon May 23, 2026
This issue has had no activity for 120 days. It will be closed in 10 days if there is no further activity. Remove the Stale label or leave a comment to keep it open.
MartinZikmund commentedon Oct 5, 2026
Adding a concrete, minimal repro for one class of the divergence:
FindNamenever returns named objects that are notFrameworkElements, such asRun,Hyperlink, or a brush inResources.The same happens with plain
Name=instead ofx:Name=, and when theTextBlockis loaded into the live visual tree before callingFindName.Evidence
FindNamereturnsnullin all 6 cases (Run, Hyperlink withx:NameandName, brush inGrid.Resources, Run in a loaded tree). A control case,<Border x:Name>inside aGrid, passes.XamlReader.Loadsnippets: all of them return the named object (Run,Hyperlink,SolidColorBrush), with bothx:NameandName.FrameworkElement::FindNameImplis a namescope table lookup (CCoreServices::TryGetElementByName), so anyDependencyObjectthe parser registered by name is found. It does not walk the visual tree.Uno root cause:
IFrameworkElementHelper.FindNamewalks visual children and matchesIFrameworkElement.Nameonly, with special cases for ContentControl/UserControl content, Popup child and flyouts. It never consults aNameScope, soTextElements and resources can't be found. The runtimeXamlReaderpath also registers no names in anyNameScope.Workaround: use the generated field (compiled XAML), or walk
TextBlock.Inlines/Resourcesmanually.fix: Resolve FindName through the XAML namescope
fix: Resolve FindName through the XAML namescope
fix: Resolve FindName through the XAML namescope
fix: Resolve FindName through the XAML namescope
fix: Resolve FindName through the XAML namescope
fix: Resolve FindName through the XAML namescope
MartinZikmund commentedon Oct 11, 2026
Two more concrete divergences under this umbrella, both reproducing at master
cc485416010(Skia Desktop runtime tests, repros below). Both come from the same root cause: Uno'sFindNamewalks the live visual tree, while WinUI resolves names through the XAML namescope.WinUI behavior (C++ sources):
FrameworkElement::FindNameImplcallsCCoreServices::TryGetElementByName(name, GetHandle())(FrameworkElement_partial.cpp). That resolves the reference object's namescope owner viaCDependencyObject::GetStandardNameScopeOwnerInternal(depends.cpp) and looks the name up in that owner's table (xcpcore_namescope.cpp). It never looks at where the element currently sits in the visual tree. Not run on native WinUI, this is from reading the sources.Uno:
IFrameworkElementHelper.FindNamematches direct children's names first, then recurses into children last-to-first. There is no namescope boundary.1. A nested UserControl's
x:Nameis returned (and preferred)A UserControl declares
x:Name="Target"(inside aStackPanel) plus aContentControlnamedHost, after the panel. An instance of the same control is hosted asouter.Host.Content.Result on Skia Desktop:
Assert.AreNotSame(inner.Target, outer.FindName("Target"))fails, soFindNamereturned the nested control's element, because it sits in the last child, which is searched first. In an app this shows up as silently wrong geometry or state rather thannull.2. CommandBar commands in the closed overflow are not found
Hosted in a UserControl with
Width = 200. After layout,Bar.PrimaryCommands.OfType<AppBarButton>().Where(b => b.IsInOverflow)is non-empty, and for those buttonsroot.FindName(button.Name)returnsnull. The first overflowed button reported isThird. An overflowed command has no visual parent while the overflow popup is closed, so the tree walk never reaches it. In WinUI the command stays registered in the namescope, so it is found.Full test class (two UserControls plus
Given_FindNameScope), ready to drop intoUno.UI.RuntimeTests:Given_FindNameScope.cs
FindNameNestedProbeis<Grid><StackPanel><TextBlock x:Name="Target" x:FieldModifier="public"/></StackPanel><ContentControl x:Name="Host" x:FieldModifier="public"/></Grid>;FindNameOverflowProbeis theCommandBarabove, both withx:FieldModifier="public"on the names.Both fail on master today; both should pass once names resolve per namescope owner, so they would make good regression tests for the namescope work.
Workaround: use the generated
x:Namefield instead ofFindNameacross nesting, and look commands up inCommandBar.PrimaryCommands/SecondaryCommandsbyName.fix: Resolve FindName through the XAML namescope