Skip to content

Commit 06f4f80

Browse files
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.0
1 parent 94ef2e4 commit 06f4f80

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

‎Chefs/Business/Models/Review.cs‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics.CodeAnalysis;
12
using ReviewData = Chefs.Client.Models.ReviewData;
23

34
namespace Chefs.Business.Models;
@@ -32,10 +33,22 @@ public Review(Guid recipeId, string text)
3233
public string? PublisherName { get; init; }
3334
public DateTime Date { get; init; }
3435
public string? Description { get; init; }
35-
public ImmutableList<Guid>? Likes { get; init; }
36-
public ImmutableList<Guid>? Dislikes { get; init; }
3736
public bool UserLike { get; init; }
3837

38+
public ImmutableList<Guid>? Likes
39+
{
40+
[DynamicDependency("Count", typeof(ImmutableList<Guid>))]
41+
get;
42+
init;
43+
}
44+
45+
public ImmutableList<Guid>? Dislikes
46+
{
47+
[DynamicDependency("Count", typeof(ImmutableList<Guid>))]
48+
get;
49+
init;
50+
}
51+
3952
internal ReviewData ToData() => new()
4053
{
4154
Id = Id,

0 commit comments

Comments
 (0)