Skip to content

Align FindName with WinUIΒ #21129

Open

Description

@MartinZikmund

What would you like to be added? πŸ’‘

FindName currently does not behave the same as in WinUI

Why is this needed? πŸ€”

Should be aligned

Renderer 🎨

  • Skia
  • Native

For which platforms πŸ“±πŸ’»πŸ–₯️

All platforms 🌍

Anything else we need to know? πŸ’¬

No response

Activity

  1. added
    kind/enhancement
    triage/untriaged
    difficulty/tbd
    on Jul 23, 2025
  2. MartinZikmund commented on Jul 23, 2025

    @MartinZikmund
    MemberAuthor

    When fixing, ensure the scenarios in the linked duplicate issues are also working correctly

  3. Youssef1313 commented on Jul 25, 2025

    @Youssef1313
    Member

    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.

  4. added
    project/core-tools πŸ› οΈ
    and removed
    triage/untriaged
    difficulty/tbd
    on Nov 24, 2025
  5. MartinZikmund commented on Jan 22, 2026

    @MartinZikmund
    MemberAuthor

    Another very dangerous example:

    <!--  Navigation Buttons  -->
    <StackPanel
        Grid.Row="1"
        Padding="8"
        Orientation="Horizontal"
        Spacing="8">
        <Button
            x:Name="BackButton"
            Click="OnGoBackClick"
            Content="Go Back"
            IsEnabled="False" />
        <Button
            x:Name="NavPage1Button"
            Click="OnNavigateToPage1Click"
            Content="Navigate to Blue Page" />
        <Button
            x:Name="NavPage2Button"
            Click="OnNavigateToPage2Click"
            Content="Navigate to Green Page" />
        <Button
            x:Name="NavPage3Button"
            Click="OnNavigateToPage3Click"
            Content="Navigate to Orange Page" />
    </StackPanel>
    
    <!--  Navigation Frame  -->
    <Border
        Grid.Row="2"
        Margin="8"
        BorderBrush="Gray"
        BorderThickness="1">
        <Frame x:Name="ContentFrame" Navigated="OnFrameNavigated">
            <Frame.ContentTransitions>
                <TransitionCollection>
                    <NavigationThemeTransition />
                </TransitionCollection>
            </Frame.ContentTransitions>
        </Frame>
    </Border>

    Notice how the "BackButton" is completely outside the ContentFrame. However, when OnApplyTemplate of Frame runs, it calls:

    var spPreviousIDependencyObject = GetTemplateChild("BackButton");

    Which ends up returning the button that is outside the frame!

  6. github-actions commented on May 23, 2026

    @github-actions
    Contributor

    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.

  7. added
    stale
    on May 23, 2026
  8. removed
    stale
    on May 25, 2026
  9. MartinZikmund commented on Oct 5, 2026

    @MartinZikmund
    MemberAuthor

    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.

    const string ns = "xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'";
    
    var tb = (TextBlock)XamlReader.Load($"<TextBlock {ns}><Run x:Name='R' Text='a'/></TextBlock>");
    tb.FindName("R");   // WinUI: Run      Uno: null
    
    var tb2 = (TextBlock)XamlReader.Load($"<TextBlock {ns}><Hyperlink x:Name='L'>x</Hyperlink></TextBlock>");
    tb2.FindName("L");  // WinUI: Hyperlink  Uno: null
    
    var g = (Grid)XamlReader.Load($"<Grid {ns}><Grid.Resources><SolidColorBrush x:Key='k' x:Name='SB' Color='Red'/></Grid.Resources></Grid>");
    g.FindName("SB");   // WinUI: SolidColorBrush  Uno: null

    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.

  10. MartinZikmund commented on Oct 11, 2026

    @MartinZikmund
    MemberAuthor

    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.

    FindNameNestedProbe outer = new();
    FindNameNestedProbe inner = new();
    outer.Host.Content = inner;
    TestServices.WindowHelper.WindowContent = outer;
    await TestServices.WindowHelper.WaitForLoaded(outer);
    
    outer.FindName("Target"); // WinUI: outer.Target   Uno: inner.Target

    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

    <CommandBar x:Name="Bar" DefaultLabelPosition="Right">
        <AppBarButton x:Name="First" Label="First" Icon="Home" />
        <AppBarButton x:Name="Second" Label="Second" Icon="Home" />
        <AppBarButton x:Name="Third" Label="Third" Icon="Home" />
        <AppBarButton x:Name="Fourth" Label="Fourth" Icon="Home" />
    </CommandBar>

    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]
    public class Given_FindNameScope
    {
    	[TestMethod]
    	public async Task When_Nested_UserControl_Declares_Same_Name_Then_Outer_Own_Element_Is_Returned()
    	{
    		FindNameNestedProbe outer = new();
    		FindNameNestedProbe inner = new();
    		outer.Host.Content = inner;
    
    		TestServices.WindowHelper.WindowContent = outer;
    		await TestServices.WindowHelper.WaitForLoaded(outer);
    
    		Assert.AreNotSame(inner.Target, outer.FindName("Target"));
    		Assert.AreSame(outer.Target, outer.FindName("Target"));
    	}
    
    	[TestMethod]
    	public async Task When_CommandBar_Command_Is_In_Closed_Overflow_Then_FindName_Finds_It()
    	{
    		FindNameOverflowProbe root = new() { Width = 200 };
    		TestServices.WindowHelper.WindowContent = root;
    		await TestServices.WindowHelper.WaitForLoaded(root);
    		await TestServices.WindowHelper.WaitForIdle();
    
    		var overflowed = root.Bar.PrimaryCommands.OfType<AppBarButton>().Where(b => b.IsInOverflow).ToList();
    		Assert.IsTrue(overflowed.Count > 0);
    
    		foreach (var button in overflowed)
    		{
    			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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/enhancementproject/core-tools πŸ› οΈ

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions