JordanMarr / ReactiveElmish.Avalonia

Static Avalonia views for Elmish programs
Other
92 stars 8 forks source link

Suggestion: Find a way to implement bindings that can be used in Avalonia's compiled binding feature. #15

Closed kgday closed 9 months ago

kgday commented 1 year ago

Probably not an easy thing to do, but would be a handy and useful feature when designing ui's is to have compiled bindings and have intellisense and compiler checking on the 'ViewModel''s members.

JordanMarr commented 1 year ago

Can you give me an example of what this might look like?

kgday commented 1 year ago

Essentially as the compiled guidelines of the Avalonia docs. https://docs.avaloniaui.net/docs/next/basics/data/data-binding/compiled-bindings#enable-and-disable-compiled-bindings

you either set the option on an individual axaml file or on a project basis. and then set the x:DataType attribute on the root node.

Code below taken from the docs when setting compiled bindings on the individual xaml file.

<!-- Set DataType and enable compiled bindings -->
<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="using:MyApp.ViewModels"
             x:DataType="vm:MyViewModel"
             x:CompileBindings="True">

So some way of creating bindings that implements a user defined viewmodel interface might work?

JordanMarr commented 1 year ago

I like the idea, but it does sound hard. It would essentially involve rethinking and replacing the entire engine.

kgday commented 1 year ago

It may be able to be achieved with some sort of api connected to the currently internal Dictionary view model. A simplistic concept is that the user could create the interface and wire the dictionary view model to the interface members some how. But my lack of knowledge in this is probably showing. Just like the old saying: 'Better to be thought a fool than to open one's mouth and remove all doubt" :-)

JordanMarr commented 1 year ago

I share your lack of knowledge regarding the core binding system since it was developed by the Elmish.WPF contributors. I learned just enough to hack it to work for Avalonia and then promptly forgot most of it. 😆

If the current bindings look like this:

let bindings ()  : Binding<Model, Msg> list = [
    "Version" |> Binding.oneWay (fun m -> m.Version)
    "Ok" |> Binding.cmd Ok
]

Maybe it would look something like this:

let mapBindings (m: Model)  = 
    { 
        Version = m.Version
        Ok = fun () -> dispatch Ok
    }

Although I imagine that the bindings object would need to implement INotifyProperyChanged as well, just like a typical VM.

Btw, that's a very nice time app you created with Avalonia.FuncUI.

kgday commented 1 year ago

Although I imagine that the bindings object would need to implement INotifyProperyChanged as well, just like a typical VM.

Yes probably. A bit of a challenge possibly to keep it functional rather than have the user use object inheritance. I am still a relative newcomer to f#

Btw, that's a very nice time app you created with Avalonia.FuncUI.

Thank you. My first real 'learning' app with f# and Elmish.

kgday commented 9 months ago

Well Done.