Repository navigation
Description
Current behavior 🐛
Name is treated as a directive in WinUI (DependencyObject_Name), and runtime lookups by name deliberately skip it. Uno resolves it like an ordinary dependency property in some places and ignores it in another, so behavior differs from WinUI in both directions.
Uno is more lenient than WinUI (the Name DP can be found by name on a FrameworkElement):
Scenario (on a Border) |
WinUI | Uno |
|---|---|---|
Binding { Source = border, Path = "Name" }, then border.Name = "B" |
target keeps "A" (CLR property read, no change notification) |
target updates to "B" (the DP is resolved and observed) |
Binding with Path="(FrameworkElement.Name)" |
throws ArgumentException (0x80070057) |
resolves and binds |
Storyboard.TargetProperty="Name" or "(FrameworkElement.Name)", then Begin() |
throws COMException 0x800F1000 |
no exception, the name is animated |
Uno is stricter than WinUI in the runtime XAML parser (XamlReader.Load): a plain Name="..." attribute on a DependencyObject that is not a FrameworkElement is silently dropped.
<Grid>
<Grid.Resources>
<MenuFlyout x:Key="k" Name="LPM" />
</Grid.Resources>
</Grid>WinUI registers the name (grid.FindName("LPM") returns the MenuFlyout); Uno returns null. Compiled XAML is not affected (WinUI's XAML compiler rejects this markup with WMC0011), so this is limited to XamlReader/hot reload. x:Name works on both.
Expected behavior 🎯
Match WinUI:
- By-name lookups in bindings and storyboard target paths must not resolve the
Namedirective.Path=Namebinds to the CLR property (no live update), and the attached-style(FrameworkElement.Name)path andStoryboard.TargetPropertyonNamefail as they do in WinUI. - The runtime XAML parser must resolve a plain
Nameattribute on anyDependencyObject, not only onFrameworkElement.
How to reproduce it (as minimally and precisely as possible) 🔬
Add this test class to src/Uno.UI.RuntimeTests and run it on Skia Desktop (/runtime-tests). Every test asserts the WinUI behavior, so all five cases fail on Uno today.
[TestClass]
[RunsOnUIThread]
public class Given_NameDirective
{
private const string Ns = "xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'";
[TestMethod]
public void When_Binding_Path_Name_Then_No_Live_Update()
{
Border border = new() { Name = "A" };
TextBlock target = new();
target.SetBinding(TextBlock.TextProperty, new Binding { Source = border, Path = new PropertyPath("Name") });
Assert.AreEqual("A", target.Text);
border.Name = "B";
Assert.AreEqual("A", target.Text); // Uno: "B"
}
[TestMethod]
public void When_Binding_Path_Attached_FrameworkElement_Name_Then_Throws()
{
Border border = new() { Name = "A" };
TextBlock target = new();
Assert.ThrowsExactly<ArgumentException>(() =>
{
target.SetBinding(TextBlock.TextProperty, new Binding { Source = border, Path = new PropertyPath("(FrameworkElement.Name)") });
_ = target.Text;
});
}
[TestMethod]
[DataRow("Name")]
[DataRow("(FrameworkElement.Name)")]
public void When_Storyboard_Targets_Name_Then_Begin_Throws(string path)
{
Border border = new() { Name = "Anim0" };
ObjectAnimationUsingKeyFrames animation = new();
animation.KeyFrames.Add(new DiscreteObjectKeyFrame { KeyTime = TimeSpan.Zero, Value = "Animated" });
Storyboard.SetTarget(animation, border);
Storyboard.SetTargetProperty(animation, path);
Storyboard storyboard = new();
storyboard.Children.Add(animation);
try
{
Assert.ThrowsExactly<System.Runtime.InteropServices.COMException>(() => storyboard.Begin());
}
finally
{
storyboard.Stop();
}
}
[TestMethod]
public void When_XamlReader_Plain_Name_On_NonFrameworkElement_Then_Registered()
{
var grid = (Grid)XamlReader.Load($"<Grid {Ns}><Grid.Resources><MenuFlyout x:Key='k' Name='LPM'/></Grid.Resources></Grid>");
Assert.IsInstanceOfType<MenuFlyout>(grid.FindName("LPM")); // Uno: null
}
}Result on Uno master (Skia Desktop): 5 of 5 fail. Binding case: the target reads "B". The attached-path and both storyboard cases: no exception is thrown. The XamlReader case: FindName returns null.
Workaround 🛠️
- Lenient direction: none needed. Use a regular property if an app relies on
{Binding Name, ElementName=x}updating live. - Strict direction: use
x:Nameinstead ofNamein XAML loaded throughXamlReader.
Works on UWP/WinUI
Works on WinUI (WinAppSDK 2.4.0). The WinUI column above was observed in a native WinUI app, and the cause is confirmed in the WinUI C++ sources.
Affected platforms 📱💻🖥️
Skia (all heads share this code). Verified at runtime on Skia Desktop.
Uno.Sdk version (and other relevant versions) 📦
Uno master @ 0599dad (net11.0).
Anything else we need to know? 💬
WinUI side (C++ sources and native runtime check)
KnownPropertyIndex::DependencyObject_Nameis flagged as a directive:TypeTableStructs.h:342(IsDirective()).MetadataAPI::TryGetDependencyPropertyByNamedefaults toallowDirectives = false(MetadataAPI.h:165-169) and skips directives (MetadataAPI.cpp:1292). Binding and storyboard target-path resolution use that default, soNameis not found as a DP.- The parser path passes
allowDirectives = true(MetadataAPI.cpp:1150, exercised byCanResolveDirectiveOnCustomTypeinMetadataUnitTests.cpp), which is why a plainNameresolves inXamlReaderon any nativeDependencyObject. - The table values were observed in a small unpackaged WinAppSDK 2.4.0 app. It also showed that a custom
DependencyObjectwith its own CLRNameproperty, set via plainName=, gets only the CLR property set (no name-scope entry); Uno matches that case.
Uno side (source on master, 0599dad)
FrameworkElement.NamePropertyis a regular registered DP (FrameworkElement.cs:870), and the by-name lookup (DependencyProperty.InternalGetProperty) has no notion of directives, so binding and storyboard resolution find it.- The runtime parser only routes
x:Name(member with no declaring type) to the name-scope registration (XamlObjectBuilder.cs:595-608). A plainNamegoes throughTypeResolver.FindDependencyProperty, which finds nothing on a non-FrameworkElement.
Notes
- Severity is low. The lenient direction is harmless in practice, and the strict direction only affects
XamlReader/hot reload. - Related but different: Name change callback isn't raised when the same name is written again (WinUI raises it) #25213 (the
Namechange callback is not raised when the same name is written again). - PR fix: Declare Name on DependencyObject like WinUI #25184 (declare
NameonDependencyObject) is open and touches the same area. This issue is about the by-name lookup semantics, which that PR does not change forFrameworkElement.
Activity