Context: 559b1f7
Context: https://github.com/unoplatform/uno/issues/20533#issuecomment-3752449412
(Repetition is fun!)
While running the uno.chefs app on macOS under NativeAOT:
dotnet publish -c Release -r osx-x64 -f net10.0-desktop -p:TargetFrameworkOverride=net10.0-desktop \
-bl Chefs/Chefs.csproj -p:SelfContained=true -p:UseSkiaRendering=true\
-p:PublishAot=true -p:IsAotCompatible=true \
-p:IlcGenerateMapFile=true -p:IlcGenerateMstatFile=true -p:IlcGenerateDgmlFile=true -p:IlcGenerateMetadataLog=true \
-p:EmitCompilerGeneratedFiles=true -p:CompilerGeneratedFilesOutputPath=`pwd`/_gen
Chefs/bin/Release/net10.0-desktop/osx-x64/publish/Chefs
while using Uno 6.5.0-dev.104 (and later), the Login page doesn't
work properly. Specifically, after launching the app, click **Skip**
to get to the Login page, and:
* The `Username` field can be selected, but text cannot be entered.
* The `Password` field cannot be selected via the mouse, but you
can tab to it from the `Password` field.
* You cannot enter text into the `Password` field.
* You cannot check the `[ ] Remember me` checkbox.
You *can* click the **Sign in with Apple** and **Sign in with Google**
buttons.
Why don't the `<TextBox/>` or `<PasswordBox/>` controls work?
Given that this is only under NativeAOT, the obvious answer is
"Reflection!", but from where?
The less obvious answer is *event registration*, specifically the
interaction between `Control.SubscribeToPostKeyDown()` and
`UIElement.GetIsEventOverrideImplemented(Type, string, Type[])`:
partial class Control {
[UnconditionalSuppressMessage("Trimming", "IL2072", Justification = "Types manipulated here have been marked earlier")]
private void SubscribeToPostKeyDown() {
if (GetIsEventOverrideImplemented(GetType(), nameof(OnPostKeyDown), _keyArgsType))
PostKeyDown += OnPostKeyDownHandler;
}
}
partial class UIElement {
private protected static bool GetIsEventOverrideImplemented(
[DynamicallyAccessedMembers(…)] Type type, string name, Type[] args)
{
var method = type.GetMethod(name, BindingFlags.NonPublic | BindingFlags.Instance, null, args, null);
return method != null && method.IsVirtual
&& method.DeclaringType != typeof(UIElement)
&& method.DeclaringType != typeof(Control);
}
}
In order for text input to work, the type must subscribe to the
`PostKeyDown` event. In order for e.g. `<TextBox/>` to subscribe to
the `PostKeyDown` event, it must override the `OnPostKeyDown()`
method, *and* `Control` must *determine* that `OnPostKeyDown()` was
overridden. This override check involves Reflection by way of
`Type.GetMethod()`, which in turn requires that `OnPostKeyDown` be
included in "reflection metadata."
It was not:
% grep OnPostKeyDown Chefs/obj/Release/net10.0-desktop/osx-x64/native/Chefs.metadata.csv
3432e2bf, ConstantStringValue, "OnPostKeyDown", ""
We need *more* than `ConstantStringValue` for it be useful.
For example, if we add `[DynamicDependency]` to `OnPostKeyDown()`:
partial class TextBox {
[DynamicDependency(nameof(OnPostKeyDown))]
private protected override void OnPostKeyDown(KeyRoutedEventArgs args) => …;
}
that *fixes* the "keyboard input doesn't work" problem, and
`Chefs.metadata.csv` contains:
% grep OnPostKeyDown Chefs/obj/Release/net10.0-desktop/osx-x64/native/Chefs.metadata.csv
5009119c, Method, "[HasThis] System.Void OnPostKeyDown(Microsoft.UI.Xaml.Input.KeyRoutedEventArgs)", "34117ead 56117ebb 6202258a 42117ec7"
34117ead, ConstantStringValue, "OnPostKeyDown", ""
42117ec7, CustomAttribute, "System.Diagnostics.CodeAnalysis.DynamicDependencyAttribute.[HasThis] System.Void .ctor(System.String)(\"OnPostKeyDown\")(ctor: Internal.Metadata.NativeFormat.Handle", "6c0aa81f 34117ead"
However, this is not a scalable answer. *Lots* of codepaths touch
`GetIsEventOverrideImplemented()`, notably
`Control.EvaluateImplementedControlRoutedEvents(Type)`.
Do we really want (or need?) to add every `Control` method to
reflection metadata?
Additionally, some parts of System.Reflection *do not require*
"reflection metadata". In particular, it is possible to obtain
`MethodInfo` instances *without* using `Type.GetMethod()`,
by using *delegates*:
Action a = () => {};
MethodInfo method = a.Method;
This does not require "reflection metadata", and works under NativeAOT.
Rework `Control.EvaluateImplementedControlRoutedEvents()` et al to
use *delegates* instead of `Type.GetMethod()`:
partial class UIElement {
private protected static bool GetIsEventOverrideImplemented<T>(Action<T> a) =>
GetIsEventOverrideImplemented((Delegate)a);
private protected static bool GetIsEventOverrideImplemented(Delegate d) =>
d.Method.IsVirtual
&& method.DeclaringType != typeof(UIElement)
&& method.DeclaringType != typeof(Control);
}
partial class Control {
private void SubscribeToPostKeyDown() {
if (GetIsEventOverrideImplemented(OnPostKeyDown))
PostKeyDown += OnPostKeyDownHandler;
}
}
This works because delegates know about method overrides.
This not only works under NativeAOT, but also allows us to remove
several `[UnconditionalSuppressMessage]` annotations.
However, in order for this to work, many methods which were previously
`static` must now be instance methods, such as
`Control.EvaluateImplementedControlRoutedEvents()`.
Context: 559b1f7
Context: https://github.com/unoplatform/uno-private/issues/1697#issuecomment-3752449412
(Repetition is fun!)
While running the uno.chefs app on macOS under NativeAOT:
while using Uno 6.5.0-dev.104 (and later), the Login page doesn't work properly. Specifically, after launching the app, click Skip to get to the Login page, and:
Usernamefield can be selected, but text cannot be entered.Passwordfield cannot be selected via the mouse, but you can tab to it from thePasswordfield.Passwordfield.[ ] Remember mecheckbox.You can click the Sign in with Apple and Sign in with Google buttons.
Why don't the
<TextBox/>or<PasswordBox/>controls work?Given that this is only under NativeAOT, the obvious answer is "Reflection!", but from where?
The less obvious answer is event registration, specifically the interaction between
Control.SubscribeToPostKeyDown()andUIElement.GetIsEventOverrideImplemented(Type, string, Type[]):In order for text input to work, the type must subscribe to the
PostKeyDownevent. In order for e.g.<TextBox/>to subscribe to thePostKeyDownevent, it must override theOnPostKeyDown()method, andControlmust determine thatOnPostKeyDown()was overridden. This override check involves Reflection by way ofType.GetMethod(), which in turn requires thatOnPostKeyDownbe included in "reflection metadata."It was not:
We need more than
ConstantStringValuefor it be useful. For example, if we add[DynamicDependency]toOnPostKeyDown():that fixes the "keyboard input doesn't work" problem, and
Chefs.metadata.csvcontains:However, this is not a scalable answer. Lots of codepaths touch
GetIsEventOverrideImplemented(), notablyControl.EvaluateImplementedControlRoutedEvents(Type).Do we really want (or need?) to add every
Controlmethod to reflection metadata?It also doesn't fix the inabilty to interract with the checkbox.
Additionally, some parts of System.Reflection do not require "reflection metadata". In particular, it is possible to obtain
MethodInfoinstances without usingType.GetMethod(), by using delegates:This does not require "reflection metadata", and works under NativeAOT.
Rework
Control.EvaluateImplementedControlRoutedEvents()et al to use delegates instead ofType.GetMethod():This works because delegates know about method overrides.
This not only works under NativeAOT, but also allows us to remove many
[UnconditionalSuppressMessage]annotations.However, in order for this to work, many methods which were previously
staticmust now be instance methods, such asControl.EvaluateImplementedControlRoutedEvents().For backward compatibility/migration purposes, preserve the previous
staticmethods and[Obsolete]them, directing code to use the instance method alternatives.GitHub Issue: closes #
PR Type:
What is the current behavior? 🤔
What is the new behavior? 🚀
PR Checklist ✅
Please check if your PR fulfills the following requirements:
Screenshots Compare Test Runresults.Other information ℹ️