Repository navigation
fix: Resolve FindName through the XAML namescope - #25307
MartinZikmund wants to merge 8 commits into
Conversation
|
Claude finished @MartinZikmund's task in 24s —— View job Review summaryI reviewed the FindName/namescope commits after Verdict: fix-first, minor. The approach is sound, and the template-part lookups now stay inside the template subtree as intended. I left 2 inline comments, both medium severity.
Cross-cutting:
|
|
|
||
| // WinUI's FindName is a lookup in the element's namescope, which also holds named non-FrameworkElement | ||
| // objects (Run, brushes, MenuFlyout...). Uno's tree walk runs first so its existing results are kept. | ||
| private object FindNameInNameScope(string name) |
There was a problem hiding this comment.
NameScope.GetNameScope(this) resolves through the inherited/parent scope, so child.FindName("x") can now return an object from an outer page or template. That contradicts the PR's "names don't cross between scopes" claim, and the new fallback has no test for this case. Either add a test that pins the intended behaviour, or document that the fallback is deliberately inherited. Note this fallback only covers the element FindName was called on, while FindNameInSubtree is used for descendants, so the two lookups behave differently.
| if (nameScope == _componentNameScope) | ||
| { | ||
| // LoadComponent re-registers the names the component's generated code already registered. | ||
| nameScope.UnregisterName(name); |
There was a problem hiding this comment.
Unregistering before re-registering hides genuine duplicate names within the component's XAML. The generated code registered the name already, so a second x:Name with the same value in the runtime XAML silently replaces the first instead of reporting a duplicate (WinUI raises a parse error). Consider unregistering only when the existing entry is the same instance, or when it came from the compiled registration, and letting RegisterName throw otherwise.
0dfdfc9 to
1643999
Compare
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-25307/wasm-skia-net9/index.html |
1643999 to
bc38c4d
Compare
|
Claude finished @MartinZikmund's task in 17s —— View job Review summary (re-review after synchronize)I re-read the FindName/namescope diff: the generator, Verdict: fix-first, minor. Nothing new beyond the earlier review. No new inline comments, to avoid duplicates. Still open from the earlier review (2 medium):
Checked, no issue found:
Not from this PR: the CodeQL |
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-25307/wasm-skia-net9/index.html |
|
🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-25307/docs/index.html |
|
The build 238968 found UI Test snapshots differences: Details
|
bc38c4d to
c46ee0e
Compare
|
Claude finished @MartinZikmund's task in 14s —— View job Review summary (re-review after synchronize)I read the diff for the generator registration, Verdict: fix-first, minor. This push adds nothing new. I posted no new inline comments, so the existing ones stay the only ones. Still open (2 medium, both already commented inline):
Cross-cutting:
|
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-25307/wasm-skia-net9/index.html |
|
The build 239014 found UI Test snapshots differences: Details
|
morning4coffe-dev
left a comment
There was a problem hiding this comment.
Verified by reading the diff at head c46ee0e against WinUI sources (dxaml/xcp/core/dll/xcpcore_namescope.cpp GetAdjustedReferenceObjectAndNamescopeType, dxaml/xcp/core/core/elements/Control.cpp CControl::GetTemplateChild, dxaml/xcp/core/Parser/XamlProperty.cpp for the DependencyObject_Name directive). The Name= directive on FrameworkElement, the template-part subtree lookups, and the per-XamlReader-load and per-template namescope ownership all match WinUI. Uno's IXNameProvider covers only TextElement/VisualState/VisualStateGroup, so the new generator branch is reached only for FrameworkElement Name= and does not overlap the x:Name path. Nothing was built or run by me. Reviewed by inspection only.
Findings, most severe first:
-
Medium, namescope leak through public FindName (
src/Uno.UI/UI/Xaml/FrameworkElement.Interface.cs:35,39-40).NameScope.NameScopePropertyis registered withFrameworkPropertyMetadataOptions.Inherits(NameScope.cs), soNameScope.GetNameScope(this)resolves through ancestors. Any element in a template or subtree that has no namescope of its own (for example a ControlTemplate built in C#, or elements under such a root) will now return names from the outer page's namescope from publicFindName. WinUI resolves a templated member through its TemplatedParent's template namescope and returns null for outer names. The XAML-loaded template path is covered byWhen_Live_ControlTemplate_Then_Names_Do_Not_Cross_Scopes, but nothing covers the code-built case, and the PR body's "names don't cross scopes" claim is not true for it. Either add a test for that case or narrow the fallback and document the gap. -
Low/medium, duplicate-name errors are masked (
src/Uno.UI/UI/Xaml/Markup/Reader/XamlObjectBuilder.cs:501-507). For the component scope, the code unregisters the name and then re-registers it.NameScope.RegisterNameonly logs a warning on duplicates, so genuine duplicates in LoadComponent disappear. Suggested change: whennameScope.FindName(name)already returns the sameinstance, skip the register; otherwise register so a real duplicate still warns. This removes the need for the unregister. -
Low, the generator
Name=branch skips validation (src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs:3540-3549). The newelse ifregistersvaluewithoutValidateName(...), unlike the x:Name branch above it. Invalid identifiers then fail in the generated__that.{value}code instead of as aXamlGenerationException. This predates the PR for the field path, but the new registration is the first place the value goes into the namescope unchecked. Suggest callingValidateNamefor consistency. -
Open question, screenshot-compare results. Bot comments on this PR (builds 238968 and 239014) report 118 changed on skia-linux and 2458 changed on skia-windows. The checklist box for screenshot validation is still unchecked. A namescope-only change should not produce that many rendering diffs, so please confirm they come from the base (#23935) or from CI noise before this merges.
-
Open question, validation at this head. The PR body cites runtime results at earlier stack heads (#25184 and #25308), and says Uno.UI compiles at this head. I did not find a runtime pass stated for c46ee0e itself. Please run the FindName and template-part tests (
Given_FindName_NonFrameworkElement, plusGiven_DependencyObject_NameandGiven_VisualStateManager) at this head and say so.
Also note, not blocking: FindName still runs the tree walk first, so a same-named FrameworkElement in the tree still wins over the namescope entry. The PR documents this as deferred to #21129, so no change is needed here.
Items 1 to 3 match the open points from the earlier automated review on this PR. I have added the code-built template case and the masking detail.
FindName falls back to the element's namescope when the tree walk finds nothing, so named Runs, Hyperlinks, brushes and flyouts resolve as on WinUI. Recursion stays a pure tree walk so names don't leak across scopes. XamlReader now builds namescopes like compiled XAML: one owned by the parsed root, and one per template materialization. Part of #21129. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017TzDba3gSK3uYpvLmmBmaa
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017TzDba3gSK3uYpvLmmBmaa
GetTemplateChild, its IFrameworkElement extension and the DescriptionPresenter/LightDismissLayer lookups now walk the control's own subtree only. The FindName namescope fallback reads the inherited NameScope, which for a control is its page's or outer template's scope, so a missing template part resolved to an unrelated element of the same name. WinUI's GetTemplateChild only searches the template namescope. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017TzDba3gSK3uYpvLmmBmaa
Replace the detached DataTemplate check with a live ControlTemplate: names must not cross between the page and template scopes, and GetTemplateChild must not return an outer element for a missing part. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017TzDba3gSK3uYpvLmmBmaa
FrameworkElement.Name is WinUI's DependencyObject_Name, which the parser treats as the name directive, so Name= is registered like x:Name. XamlReader already did; compiled XAML now matches. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017TzDba3gSK3uYpvLmmBmaa
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017TzDba3gSK3uYpvLmmBmaa
NameScope is Uno-internal, so the WinAppSDK runtime-test build failed with CS0103. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017TzDba3gSK3uYpvLmmBmaa
These assert x:Name storage on non-FrameworkElement objects, which needs DependencyObject_Name from #25184. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017TzDba3gSK3uYpvLmmBmaa
c46ee0e to
80d4676
Compare
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
|
Claude finished @MartinZikmund's task in 13s —— View job Review summary (re-review after synchronize, by inspection only; nothing built or run)Verdict: fix-first, minor. No new problems beyond the open points already raised. Three of them overlap with morning4coffe-dev's review. Open (all previously raised):
Checked, no issue found: commit messages follow Conventional Commits, no tests are skipped or ignored, and the template-part lookups now stay inside the template subtree. The CodeQL |
|
🤖 Your WebAssembly Sample App stage site is ready! Visit it here: |
|
The build 245163 found UI Test snapshots differences: Details
|
morning4coffe-dev
left a comment
There was a problem hiding this comment.
I went over this again after the rebase onto master. As far as I can see, the points from my earlier review are still open: the inherited-scope fallback in FindName, the unregister that hides duplicate names, and the missing ValidateName for Name=. I also checked the runtime-name handling against WinUI's parser (XamlManagedTypeInfoProvider::GetRuntimeNameProperty always returns DependencyObject_Name) and left one new note on XamlReader below.
| && member.Value is string { Length: > 0 } name | ||
| && instance is not MarkupExtension | ||
| && (member.Member.DeclaringType == null | ||
| || (TypeResolver.IsType(control.Type, member.Member.DeclaringType) && !TypeResolver.IsAttachedProperty(member)))) |
There was a problem hiding this comment.
For a plain Name=, this matches any type with its own CLR Name property, so a POCO such as <local:Item Name="Home"/> gets registered in the namescope and becomes visible to FindName. In WinUI the runtime name property is always DependencyObject_Name (core/Parser/ManagedTypeInfoProvider.cpp / NativeTypeInfoProvider.cpp, GetRuntimeNameProperty), and the compiled path here already restricts it through IsFrameworkElementNameMember. Could we limit the non-x: case to instance is FrameworkElement and keep DeclaringType == null (x:Name) as it is? A small XamlReader test with a non-DO item that has a Name property, ideally with a duplicate value, would pin it down.
|
Tick the box to add this pull request to the merge queue (same as
|
GitHub Issue: part of #21129
Note
Stacked on #23935 (base branch
005-mobile-a11y-automation, GitHub stack #24389). The diff shows only this PR's commits.Stack, bottom to top:
NamePropertyPR Type:
🐞 Bugfix
What changed? 🚀
FindNamenever returned named objects that aren'tFrameworkElements, such asRun,Hyperlink, brushes, transforms and flyouts. WinUI finds all of them, for bothx:NameandName=. These commits were split out of #23935 unchanged.GetTemplateChildand the internalDescriptionPresenter/LightDismissLayerlookups search only the control's template subtree, never the inherited outer namescope. WinUI'sCControl::GetTemplateChild(core/core/elements/Control.cpp) searches the template namescope only.x:NameandFrameworkElement.Name=are registered in the namescope, in compiled XAML and in XamlReader. WinUI's parser treatsDependencyObject_Nameas the name directive. XamlReader now builds namescopes the same way compiled XAML does: one owned by the parsed root, and one per template materialization.Adaptation for this stack position
Three tests assert that
x:Nameon a non-FrameworkElement is stored inFrameworkElement.NameProperty(When_XName_On_NonFrameworkElement_Then_Name_Property_Is_Set,When_Compiled_XName_On_NonFrameworkElement_Then_Name_Property_Is_Set,When_NameProperty_Set_On_DependencyObject_Then_No_AutomationId_Side_Effect). That storage comes from #25184, sotest: Move DO Name storage tests to #25184removes them here and #25184 restores them unchanged.Deferred to #21129
TryGetElementByName). Here the tree walk still runs first. The namescope owner model in refactor(namescope): Introduce the WinUI namescope owner model #24473 should land before that port..Resourcesare.Name=: in XamlReader, a plainName=on a DependencyObject type with noNameproperty is still dropped.Validation
net10.0):Uno.UIbuilds with 0 errors at this head.net10.0-desktop): at this level (previous restack, same commits), the 15 FindName tests that don't need DO Name storage passed. The three DO Name storage tests failed here as expected before the adaptation commit. At the fix: Declare Name on DependencyObject like WinUI #25184 head, all 18 FindName tests pass. At the fix(a11y): Use owning flyout name for presenter AutomationId #25308 head, 80/80 pass, including FindName,Given_DependencyObject_Name,Given_VisualStateManager,Given_MenuFlyoutand the Flyout presenter AutomationId tests.PR Checklist ✅
Screenshots Compare Test Runresults.🤖 Generated with Claude Code
https://claude.ai/code/session_017TzDba3gSK3uYpvLmmBmaa