Repository navigation
fix: display of Likes and Dislikes counts under NativeAOT - #1729
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a NativeAOT reflection issue where the Count property on ImmutableList<Guid> was not accessible during runtime, causing like and dislike counts to not display in the Reviews tab. The solution adds the [Microsoft.UI.Xaml.Data.Bindable] attribute to all model types in Chefs/Business/Models that may be used via XAML data binding, ensuring their properties (and nested type properties) are included in reflection metadata.
Changes:
- Added
[Microsoft.UI.Xaml.Data.Bindable]attribute to 21 model classes to ensure NativeAOT reflection metadata includes properties accessed through XAML bindings - Establishes a consistent convention that all model types used in XAML should be marked with
[Bindable]
Reviewed changes
Copilot reviewed 20 out of 20 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| Chefs/Business/Models/AppConfig.cs | Added [Bindable] attribute to AppConfig model |
| Chefs/Business/Models/Category.cs | Added [Bindable] attribute to Category model |
| Chefs/Business/Models/CategoryWithCount.cs | Added [Bindable] attribute to CategoryWithCount model |
| Chefs/Business/Models/Cookbook.cs | Added [Bindable] attribute to Cookbook model |
| Chefs/Business/Models/CookbookImages.cs | Added [Bindable] attribute to CookbookImages model |
| Chefs/Business/Models/Credentials.cs | Added [Bindable] attribute to Credentials model |
| Chefs/Business/Models/DialogInfo.cs | Added [Bindable] attribute to DialogInfo model |
| Chefs/Business/Models/GroupedNotification.cs | Added [Bindable] attribute to GroupedNotification model |
| Chefs/Business/Models/Ingredient.cs | Added [Bindable] attribute to Ingredient model |
| Chefs/Business/Models/Iterator.cs | Added [Bindable] attribute to StepIterator model |
| Chefs/Business/Models/Notification.cs | Added [Bindable] attribute to Notification model |
| Chefs/Business/Models/Nutrition.cs | Added [Bindable] attribute to Nutrition model |
| Chefs/Business/Models/NutritionChartItem.cs | Added [Bindable] attribute to NutritionChartItem model |
| Chefs/Business/Models/Recipe.cs | Added [Bindable] attribute to Recipe model |
| Chefs/Business/Models/Review.cs | Added [Bindable] attribute to Review model (primary fix for likes/dislikes count display) |
| Chefs/Business/Models/SearchFilter.cs | Added [Bindable] attribute to SearchFilter model |
| Chefs/Business/Models/SearchHistory.cs | Added [Bindable] attribute to SearchHistory model |
| Chefs/Business/Models/Step.cs | Added [Bindable] attribute to Step model |
| Chefs/Business/Models/UpdateCookBook.cs | Added [Bindable] attribute to UpdateCookbook model |
| Chefs/Business/Models/User.cs | Added [Bindable] attribute to User model |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| namespace Chefs.Business.Models; | ||
|
|
||
| [Microsoft.UI.Xaml.Data.Bindable] |
There was a problem hiding this comment.
The PR description contains "GitHub Issue (If applicable): #" without an actual issue number. According to the mandatory issue linking rules, every PR must reference a proper GitHub issue using the format "Fixes https://github.com/unoplatform//issues/#<ISSUE_NUMBER>". Please update the PR description to include the actual issue number or link.
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
f494d87 to
0666890
Compare
|
The attempt to put This appears to be a variation on: microsoft/microsoft-ui-xaml#8723 This unfortunately means the only viable fix -- while still supporting builds on WinRT -- is to use PR has been updated. |
Context: unoplatform/uno@559b1f7
Build and run
Chefs.csprojfor e.g. net10.0-desktop and NativeAOT while using Uno 6.6.0-dev.3:From the launch screen:
Console output contains the message:
Additionally, no count is shown for likes or dislikes. Instead of:
we see:
The cause, of course, is missing reflection metadata (unoplatform/uno@559b1f78), and the Console output
failmessage is what connects the two.The use of
ImmutableList<T>.Countis fromItemTemplates.xaml, e.g.:The question then becomes, how do we fix this?
There are (at least) two options for adding
ImmutableList<T>.Countto reflection metadata:Via
[DynamicDependency], a'la:Via
[Bindable], a'la:(2) works because the
Uno.UI.SourceGenerators.BindableTypeProvidersource generator emitsBindableMetadata.g.cs, which contains:The
BindableTypeconstructor causes all properties onReviewto be added to reflection metadata, and thebindableType.AddProperty("Likes", typeof(ImmutableList<Guid>))invocation causes all properties onImmutableList<Guid>to be added to reflection metadata, includingCount.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].On the assumption that all "Model" types may be used via XAML, add
[Bindable]to all types withinChefs/Business/Models.GitHub Issue (If applicable): #
PR Type
What kind of change does this PR introduce?
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
Internal Issue (If applicable):