Skip to content

Native AOT: Attempt to support attached properties #23133

Closed

Description

@jonpryor

What would you like to be added? 💡

Attached properties are DependencyProperty instances which use DependencyProperty.RegisterAttached(), such as:

https://github.com/unoplatform/Uno.Gallery/blob/061dac15260bac98ea3eb2210887feb2fabfa2a7/Uno.Gallery/Extensions/XamlDisplayExtensions.cs#L31-L38

namespace ShowMeTheXAML {

	partial class XamlDisplayExtensions {

		public static DependencyProperty HeaderProperty { get; } = DependencyProperty.RegisterAttached(
			"Header",
			typeof(string),
			typeof(XamlDisplayExtensions),
			new PropertyMetadata(default));

		public static string GetHeader(XamlDisplay obj) => (string)obj.GetValue(HeaderProperty);
		public static void SetHeader(XamlDisplay obj, string value) => obj.SetValue(HeaderProperty, value);
	}
}

The problem, from trimming or Native AOT perspectives, is that while HeaderProperty is preserved, there is no static connection between HeaderProperty, GetHeader, and SetHeader. Consequently, the GetHeader and SetHeader methods are likely to be removed.

The workaround is to use DynamicDependencyAttribute to connect HeaderProperty to it's dependencies:

https://github.com/unoplatform/Uno.Gallery/blob/54a08b1587aa020a5651d61eb3a3023a79e89d72/Uno.Gallery/Extensions/XamlDisplayExtensions.cs#L32-L44

namespace ShowMeTheXAML {

	partial class XamlDisplayExtensions {

		public static DependencyProperty HeaderProperty
		{
			[DynamicDependency(nameof(GetHeader))]
			[DynamicDependency(nameof(SetHeader))]
			get;
		} = DependencyProperty.RegisterAttached(
			"Header",
			typeof(string),
			typeof(XamlDisplayExtensions),
			new PropertyMetadata(default));

		public static string GetHeader(XamlDisplay obj) => (string)obj.GetValue(HeaderProperty);
		public static void SetHeader(XamlDisplay obj, string value) => obj.SetValue(HeaderProperty, value);
	}
}

While this works, the problem is that this is tedious, and means that significant changes may be needed make apps work properly under Native AOT.

Proposal

The "100% correct" solution would be to scan IL looking for DependencyProperty.RegisterAttached() invocations, look at the first parameter, and automatically preserve methods with Get and Set prefixes on that value.

However, this approach is cumbersome. It's possible, but somewhat brittle.

As an easier -- if less correct -- solution: update the <BindableTypeLinkerGeneratorTask/> task (PR #22676) to check for static fields and properties of type DependencyProperty, remove the Property suffix from the name, and automatically preserve methods with Get and Set prefixes on the resulting name.

For example, given XamlDisplayExtensions.HeaderProperty:

  1. Remove the Property suffix, resulting in Header.
  2. Check to see if GetHeader and SetHeader exist in the same declaring type.
  3. For the methods from (2) which exist, update Uno.Bindable.Descriptor.xml -- or use a new file? -- to preserve those methods.

For example, Uno.Bindable.Descriptor.xml would contain the fragment:

    <type fullname="ShowMeTheXAML.XamlDisplayExtensions" required="false">
      <method name="GetHeader" />
      <method name="SetHeader" />
    </type

This heuristic may be enough to avoid the need for scanning method body IL looking for DependencyProperty.RegisterAttached() invocations.

Why is this needed? 🤔

No response

Renderer 🎨

  • Skia
  • Native

For which platforms 📱💻🖥️

No response

Anything else we need to know? 💬

No response

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

Metadata

Metadata

Labels

kind/enhancementtriage/untriaged

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions