Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom control's properties can't be used by xaml outside the control's own assembly (F#) #17064

Open
marklam opened this issue Sep 19, 2024 · 0 comments
Labels

Comments

@marklam
Copy link

marklam commented Sep 19, 2024

Describe the bug

Avalonia bindings expect the properties of a control to be defined as static fields

e.g. C#

    public static readonly StyledProperty<string> MessageProperty = AvaloniaProperty.Register<UserControl1, string>("Message");

Which in the IL is

 .field public static initonly class [Avalonia.Base]Avalonia.StyledProperty`1<string> MessageProperty

Static fields in F# are given internal/assembly access

e.g. F#

    static let MessageProperty = AvaloniaProperty.Register<UserControl1, string>("Message")

Which in the IL is

 .field assembly static class [Avalonia.Base]Avalonia.StyledProperty`1<string> MessageProperty

If the controls project (.fsproj) has the property <ProduceReferenceAssembly>true</ProduceReferenceAssembly> then the view will be built against the controls' reference assembly, which will have had the property definition fields removed (because they were marked as internal).

Controls ProduceReferenceAssembly View AvaloniaUseCompiledBindingsByDefault Failure
false false Runtime System.FieldAccessException
false true Runtime System.FieldAccessException
true false Compiler Avalonia error AVLN3000: Unable to find suitable setter or adder for property Message of type FSharpControl:FSharpControl.UserControl1 for argument Avalonia.Markup:Avalonia.Data.Binding
true true Compiler Avalonia error AVLN3000: Unable to find suitable setter or adder for property Message of type FSharpControl:FSharpControl.UserControl1 for argument Avalonia.Markup:Avalonia.Data.Binding

To Reproduce

https://github.com/marklam/AvaloniaBindToFSharpControl contains a C# view app with equivalent F# and C# control libraries, each containing a UserControl1 with a Message Avalonia property.

The F# project also has a UserControl2 which defines a public static property-getter for the StyledProperty, which cannot be bound because it's defined as a property not a field.

Expected behavior

Since the compiled bindings will fail at runtime, the compiler should probably produce an error for an internal field being bound from a different assembly.

But most of all, it would be fantastic if the bindings could be made to a StyledProperty defined in a static property-getter, instead of only those defined in a static field, since F# can declare those easily. (See UserControl2.fs in the example repo.)

Avalonia version

11.1.3

OS

Windows

Additional context

No response

@marklam marklam added the bug label Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant