Skip to content

fix: display of Likes and Dislikes counts under NativeAOT - #1729

Merged
jonpryor merged 1 commit into
mainfrom
dev/jonpryor/jonp-fix-review-likes-count
Jan 30, 2026
Merged

jonpryor merged 1 commit into
mainfrom
dev/jonpryor/jonp-fix-review-likes-count

Conversation

@jonpryor

Copy link
Copy Markdown
Collaborator

Context: unoplatform/uno@559b1f7

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], a'la:

    partial record Review {
        public ImmutableList<Guid>? Likes
        {
            [DynamicDependency("Count", typeof(ImmutableList<Guid>))] get;
            init;
        }
    }
    
  2. Via [Bindable], 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].

On the assumption that all "Model" types may be used via XAML, add [Bindable] to all types within Chefs/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:

  • Tested code with current supported SDKs
  • Docs have been added/updated which fit documentation template. (for bug fixes / features)
  • Unit Tests and/or UI Tests for the changes have been added (for bug fixes / features) (if applicable)
  • Wasm UI Tests are not showing unexpected any differences. Validate PR Screenshots Compare Test Run results.
  • Contains NO breaking changes
  • Updated the Release Notes
  • Associated with an issue (GitHub or internal)

Other information

Internal Issue (If applicable):

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread Chefs/Business/Models/AppConfig.cs Outdated

namespace Chefs.Business.Models;

[Microsoft.UI.Xaml.Data.Bindable]

Copilot AI Jan 27, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot generated this review using guidance from organization custom instructions.
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
@jonpryor
jonpryor force-pushed the dev/jonpryor/jonp-fix-review-likes-count branch from f494d87 to 0666890 Compare January 27, 2026 20:25
@jonpryor

Copy link
Copy Markdown
Collaborator Author

The attempt to put [Bindable] on "all the things!" failed on Windows:

D:\a\1\s\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. [D:\a\1\s\Chefs\Chefs.csproj::TargetFramework=net9.0-windows10.0.19041]
D:\a\1\s\Chefs\obj\Release\net9.0-windows10.0.19041\XamlTypeInfo.g.cs(6908,13): error CS8852: Init-only property or indexer 'Review.RecipeId' can only be assigned in an object initializer, or on 'this' or 'base' in an instance constructor or an 'init' accessor. [D:\a\1\s\Chefs\Chefs.csproj::TargetFramework=net9.0-windows10.0.19041]
D:\a\1\s\Chefs\obj\Release\net9.0-windows10.0.19041\XamlTypeInfo.g.cs(6918,13): error CS8852: Init-only property or indexer 'Review.UrlAuthorImage' can only be assigned in an object initializer, or on 'this' or 'base' in an instance constructor or an 'init' accessor. [D:\a\1\s\Chefs\Chefs.csproj::TargetFramework=net9.0-windows10.0.19041]
D:\a\1\s\Chefs\obj\Release\net9.0-windows10.0.19041\XamlTypeInfo.g.cs(6928,13): error CS8852: Init-only property or indexer 'Review.CreatedBy' can only be assigned in an object initializer, or on 'this' or 'base' in an instance constructor or an 'init' accessor. [D:\a\1\s\Chefs\Chefs.csproj::TargetFramework=net9.0-windows10.0.19041]
D:\a\1\s\Chefs\obj\Release\net9.0-windows10.0.19041\XamlTypeInfo.g.cs(6938,13): error CS8852: Init-only property or indexer 'Review.PublisherName' can only be assigned in an object initializer, or on 'this' or 'base' in an instance constructor or an 'init' accessor. [D:\a\1\s\Chefs\Chefs.csproj::TargetFramework=net9.0-windows10.0.19041]
D:\a\1\s\Chefs\obj\Release\net9.0-windows10.0.19041\XamlTypeInfo.g.cs(6948,13): error CS8852: Init-only property or indexer 'Review.Date' can only be assigned in an object initializer, or on 'this' or 'base' in an instance constructor or an 'init' accessor. [D:\a\1\s\Chefs\Chefs.csproj::TargetFramework=net9.0-windows10.0.19041]
D:\a\1\s\Chefs\obj\Release\net9.0-windows10.0.19041\XamlTypeInfo.g.cs(6958,13): error CS8852: Init-only property or indexer 'Review.Description' can only be assigned in an object initializer, or on 'this' or 'base' in an instance constructor or an 'init' accessor. [D:\a\1\s\Chefs\Chefs.csproj::TargetFramework=net9.0-windows10.0.19041]
D:\a\1\s\Chefs\obj\Release\net9.0-windows10.0.19041\XamlTypeInfo.g.cs(6968,13): error CS8852: Init-only property or indexer 'Review.Likes' can only be assigned in an object initializer, or on 'this' or 'base' in an instance constructor or an 'init' accessor. [D:\a\1\s\Chefs\Chefs.csproj::TargetFramework=net9.0-windows10.0.19041]
D:\a\1\s\Chefs\obj\Release\net9.0-windows10.0.19041\XamlTypeInfo.g.cs(6988,13): error CS8852: Init-only property or indexer 'Review.Dislikes' can only be assigned in an object initializer, or on 'this' or 'base' in an instance constructor or an 'init' accessor. [D:\a\1\s\Chefs\Chefs.csproj::TargetFramework=net9.0-windows10.0.19041]
D:\a\1\s\Chefs\obj\Release\net9.0-windows10.0.19041\XamlTypeInfo.g.cs(6998,13): error CS8852: Init-only property or indexer 'Review.UserLike' can only be assigned in an object initializer, or on 'this' or 'base' in an instance constructor or an 'init' accessor. [D:\a\1\s\Chefs\Chefs.csproj::TargetFramework=net9.0-windows10.0.19041]

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 [DynamicDependency].

PR has been updated.

@jonpryor
jonpryor merged commit 06f4f80 into main Jan 30, 2026
17 checks passed
@jonpryor
jonpryor deleted the dev/jonpryor/jonp-fix-review-likes-count branch January 30, 2026 18:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants