AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.24k stars 2.19k forks source link

`{x:Static}` does not work on ResX resources unless they are `public` #16929

Open colejohnson66 opened 1 week ago

colejohnson66 commented 1 week ago

Describe the bug

It is documented that ResX localization must use PublicResXFileCodeGenerator:

image

If these resources are in an external library, this would make perfect sense; You can't access an internal property of another assembly. However, if the resource and usage are in the same assembly, there's no accessibility concerns. Regardless, you get a cryptic compiler error:

Error AVLN2000 Avalonia: Unable to resolve "Resources.CancelText" as static field, property, constant or enum value Line ##, position ##.

According to @maxkatz6, this is a bug. See: #16906

To Reproduce

Add a Resources.resx file to your project as so:

<ItemGroup>
    <!-- replace paths as appropriate -->
    <EmbeddedResource Update="Properties\Resources.resx">
        <Generator>ResXFileCodeGenerator</Generator>
        <LastGenOutput>Resources.Designer.cs</LastGenOutput>
    </EmbeddedResource>
    <Compile Update="Properties\Resources.Designer.cs">
        <DesignTime>True</DesignTime>
        <AutoGen>True</AutoGen>
        <DependentUpon>Resources.resx</DependentUpon>
    </Compile>
</ItemGroup>

Add a string resource:

<data name="MyResource" xml:space="preserve">
    <value>MyValue</value>
</data>

In that same assembly, try to use it in a UserControl view:

<!-- adjust XMLNS as necessary -->
<TextBlock
    Text="{x:Static props:Resources.MyResource}" />

Expected behavior

As the resources are in the same assembly as the UserControl, compilation should work.

Actual behavior

Error AVLN2000 Avalonia: Unable to resolve "Resources.MyResource" as static field, property, constant or enum value Line ##, position ##.

Avalonia version

11.1.3

OS

No response

Additional context

No response

workgroupengineering commented 1 week ago

I thought it was a simple thing to solve this issue, but in reality it is quite complicated.

It is not directly attributable to Avalonia but XamlX

https://github.com/kekekeks/XamlX/blob/7b8f6099a1c2240b9fa5bbf7a8a6954e99c4e556/src/XamlX/Ast/Intrinsics.cs#L116-L118

colejohnson66 commented 5 days ago

@workgroupengineering Thanks! I've opened an issue on XamlX's side linking back here.