-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Comments
Avalonia bindings require property definition to be accessible, which is why they are usually public. |
Are there workarounds? I had decided to use Avalonia instead of WPF especially for its F# support, but it doesn't seem to be well tested for F#. |
It is tested. But F# doesn't work well with conventional MVVM approaches, like bindings. Keeping traditional XAML and MVVM would still likely require you to write some C#. |
I think these packages do not leverage the visual designer in Visual studio. So no they are not a solution for me. Any idea on whether and when this issue will be fixed? |
@catamounttechnology this issue is fundamental and can't be "fixed". Language limitation can be lifted when request is implemented: |
Or, probably a decent workaround, you can enable internal visible to from the project with control to the project with XAML. This should work with any .NET language. |
wouldn't be easier for Avalonia to accept static public properties instead of only static public fields for AvaloniaProperties? |
This approach can allow you to use the visual designer: https://github.com/JordanMarr/ReactiveElmish.Avalonia I use an interface for a ViewModel, with implementations for a DesignViewModel and the real app's ViewModel. You can use the ReactiveElmish to expose model changes as bindable viewmodel properties. Then for making changes to the program model, you can implement commands to send update messages, and invoke the commands from events using https://github.com/wieslawsoltes/Avalonia.Xaml.Behaviors |
@marklam This is great work and I will try it. However I don't see (yet) how it can help in overcoming this issue. If someone wants to create a package containing custom controls with custom DependencyProperty (or StyledProperty as Avalonia calls them), that someone cannot use F# and must fallback to C# |
I did two things, for anything new I created controls with ViewModels and did evertything via the ViewModel. For the pre-existing controls that I had moved outside the Views assembly, I made a hacky script to change the visibility of the fields using Mono.Cecil. Although since there's only one view assembly in my case, the |
Is that "hacky script" shareable? |
@marklam I have used the script and it seems to work Without script (ILSpy) With script However in the main project I still Get the same binding error
|
Describe the bug
Avalonia bindings expect the properties of a control to be defined as static fields
e.g. C#
Which in the IL is
Static fields in F# are given
internal
/assembly
accesse.g. F#
Which in the IL is
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).System.FieldAccessException
System.FieldAccessException
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
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 aMessage
Avalonia property.The F# project also has a
UserControl2
which defines a public static property-getter for theStyledProperty
, 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. (SeeUserControl2.fs
in the example repo.)Avalonia version
11.1.3
OS
Windows
Additional context
No response
The text was updated successfully, but these errors were encountered: