Repository navigation
Commit 06f4f80
authored
fix: display of Likes and Dislikes counts under NativeAOT (#1729)
Context: unoplatform/uno@559b1f7
Context: microsoft/microsoft-ui-xaml#8723
Build and run `Chefs.csproj` for e.g. net10.0-desktop and NativeAOT
while using Uno 6.6.0-dev.3:
sed -i '' 's/"Uno.Sdk": ".*"/"Uno.Sdk": "6.6.0-dev.3"/g' global.json
git clean -xdf Chefs{,.Api}
dotnet publish -c Release -r osx-x64 -f net10.0-desktop -p:TargetFrameworkOverride=net10.0-desktop \
-bl Chefs/Chefs.csproj -p:UseSkiaRendering=true -p:SkiaPublishAot=true \
-p:SkiaPublishAot=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
From the launch screen:
1. Click **Skip**
2. Click ** Sign in with Apple**
3. Click **Fresh Salad Thaid**
4. Click the **Reviews** tab.
Console output contains the message:
fail: Uno.UI.DataBinding.BindingPropertyHelper[0]
The [Count] property getter does not exist on type [System.Collections.Immutable.ImmutableList`1[System.Guid]]
Additionally, no *count* is shown for likes or dislikes.
Instead of:
> 👍 3 👎 2
we see:
> 👍 👎
The cause, of course, is missing reflection metadata
(unoplatform/uno@559b1f78), and the Console output `fail` message
is what connects the two.
The use of `ImmutableList<T>.Count` is from `ItemTemplates.xaml`, e.g.:
<Button Background="{Binding UserLike, Converter={StaticResource UserLikeColorConverter}}"
Content="{Binding Likes.Count, Converter={StaticResource StringFormatter}, ConverterParameter='👍 {0}'}"
Command="{utu:AncestorBinding AncestorType=uer:FeedView,
The question then becomes, how do we fix this?
There are (at least) two options for adding `ImmutableList<T>.Count`
to reflection metadata:
1. Via [`[DynamicDependency]`][0], a'la:
partial record Review {
public ImmutableList<Guid>? Likes
{
[DynamicDependency("Count", typeof(ImmutableList<Guid>))] get;
init;
}
}
2. Via [`[Bindable]`][1], a'la:
[Bindable]
partial record Review {
}
(2) works because the `Uno.UI.SourceGenerators.BindableTypeProvider`
source generator emits `BindableMetadata.g.cs`, which contains:
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute]
static partial class MetadataBuilder_640
{
internal static global::Uno.UI.DataBinding.IBindableType Build(global::Uno.UI.DataBinding.BindableType parent)
{
var bindableType = parent ?? new global::Uno.UI.DataBinding.BindableType(10, typeof(global::Chefs.Business.Models.Review));
// …
bindableType.AddProperty("Likes", typeof(global::System.Collections.Immutable.ImmutableList<global::System.Guid>), GetLikes);
bindableType.AddProperty("Dislikes", typeof(global::System.Collections.Immutable.ImmutableList<global::System.Guid>), GetDislikes);
return bindableType;
}
}
The `BindableType` constructor causes all properties on `Review` to
be added to reflection metadata, and the
`bindableType.AddProperty("Likes", typeof(ImmutableList<Guid>))`
invocation causes all properties on `ImmutableList<Guid>` to be added
to reflection metadata, including `Count`.
The question, then: which should be preferred?
Use of `[DynamicDependency]` is asking for whack-a-mole, requiring that
you "somehow" know which properties are missing, and explicitly
annotation them, individually.
Use of `[Bindable]` is *also* a bit of whack-a-mole -- you still need
to know which types are involved! -- but it's less granular, and
makes for a useful convention:
All types which are accessed via XAML should be marked `[Bindable]`.
There is just one "minor" problem with this: *it doesn't build* when
building for Windows WinRT:
Chefs\obj\Release\net9.0-windows10.0.19041\XamlTypeInfo.g.cs(6898,13): Error CS8852: Init-only property or indexer 'Review.Id' can only be assigned in an object initializer, or on 'this' or 'base' in an instance constructor or an 'init' accessor.
This appears to be microsoft/microsoft-ui-xaml#8723, and can be
summarized as "`[Bindable]` types cannot have `init`-only properties."
Which means the only valid solution is (1), `[DynamicDependency]`.
Unfortunately.
[0]: https://learn.microsoft.com/dotnet/api/system.diagnostics.codeanalysis.dynamicdependencyattribute
[1]: https://learn.microsoft.com/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.data.bindableattribute?view=windows-app-sdk-1.01 parent 94ef2e4 commit 06f4f80
1 file changed
Lines changed: 15 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
35 | | - | |
36 | | - | |
37 | 36 | | |
38 | 37 | | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
39 | 52 | | |
40 | 53 | | |
41 | 54 | | |
| |||
0 commit comments