Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FindName does not work on elements that are not in visual tree #17020

Open
MartinZikmund opened this issue Jun 6, 2024 · 4 comments
Open

FindName does not work on elements that are not in visual tree #17020

MartinZikmund opened this issue Jun 6, 2024 · 4 comments
Labels
area/xaml-reader Categorizes an issue or PR as relevant to the XAML Reader difficulty/medium 🤔 Categorizes an issue for which the difficulty level is reachable with a good understanding of WinUI kind/bug Something isn't working project/core-tools 🛠️ Categorizes an issue or PR as relevant to core and tools project/layout 🧱 Categorizes an issue or PR as relevant to layouting and containers (Measure/Arrange, Collections,..)

Comments

@MartinZikmund
Copy link
Member

Current behavior

string rootPanelXaml =
    @"<StackPanel xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
            <Button x:Name='button' Content='button'>
                <Button.KeyboardAccelerators>
                    <KeyboardAccelerator x:Name='keyboardAccelerator' Modifiers='Control' Key='A' />
                </Button.KeyboardAccelerators>
            </Button>
            <ContentDialog x:Name='dialog'>
                <Button x:Name='dialogButton'>DialogButton</Button>
            </ContentDialog>
        </StackPanel>";

var rootPanel = (StackPanel)XamlReader.Load(rootPanelXaml);
var button = (Button)rootPanel.FindName("button");

In case of Uno, button is null

Expected behavior

In WinUI,. button is set correctly.

How to reproduce it (as minimally and precisely as possible)

No response

Workaround

No response

Works on UWP/WinUI

Yes

Environment

No response

NuGet package version(s)

No response

Affected platforms

WebAssembly, Android, iOS, Mac Catalyst, Skia (WPF), Skia (Linux X11), Skia (macOS), Skia (Linux Framebuffer), Skia (GTK), Windows (WinAppSDK)

IDE

Visual Studio 2022

IDE version

No response

Relevant plugins

No response

Anything else we need to know?

No response

@MartinZikmund MartinZikmund added kind/bug Something isn't working triage/untriaged Indicates an issue requires triaging or verification difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. project/layout 🧱 Categorizes an issue or PR as relevant to layouting and containers (Measure/Arrange, Collections,..) project/core-tools 🛠️ Categorizes an issue or PR as relevant to core and tools difficulty/medium 🤔 Categorizes an issue for which the difficulty level is reachable with a good understanding of WinUI and removed triage/untriaged Indicates an issue requires triaging or verification difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. labels Jun 6, 2024
@Youssef1313
Copy link
Member

That should be because XamlReader doesn't support x:Name. Using Name instead can work as a workaround

@Youssef1313 Youssef1313 added the area/xaml-reader Categorizes an issue or PR as relevant to the XAML Reader label Jun 12, 2024
@MartinZikmund
Copy link
Member Author

Unfortunately this is not enough - in case of KeyboardAccelerator for example, it is a plain DO and it seems that because of it, neither Name or x:Name works in this case

@Youssef1313
Copy link
Member

Ah. For non-FrameworkElements, FindName is impossible to get working without proper NameScope support. 😕

Draft PR: #16177, not very trivial one to get green though. It'll take a bit of time.

@MartinZikmund
Copy link
Member Author

Scenario to validate/test against:

XamlReader.Load:

<StackPanel xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
    <Button x:Name='focusButton' Content='Click for Button Flyout'>
        <Button.ContextFlyout>
            <Flyout x:Name='ButtonFlyout'>
                <StackPanel x:Name='flyoutStackPanel'>
                   <Button x:Name='flyoutButton1' Content='flyoutButton1'>
                        <Button.KeyboardAccelerators>
                            <KeyboardAccelerator x:Name='flyoutAccelerator1' Modifiers='Control' Key='Number1' />
                        </Button.KeyboardAccelerators>
                   </Button>
                </StackPanel>
            </Flyout>
        </Button.ContextFlyout>
    </Button>
</StackPanel>

Test FindName:

  • ButtonFlyout
  • flyoutButton1
  • focusButton
  • flyoutAccelerator1

All should be non-null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/xaml-reader Categorizes an issue or PR as relevant to the XAML Reader difficulty/medium 🤔 Categorizes an issue for which the difficulty level is reachable with a good understanding of WinUI kind/bug Something isn't working project/core-tools 🛠️ Categorizes an issue or PR as relevant to core and tools project/layout 🧱 Categorizes an issue or PR as relevant to layouting and containers (Measure/Arrange, Collections,..)
Projects
None yet
Development

No branches or pull requests

2 participants