Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Xamarin.Forms BindableProperty on a custom control in F#

I have F# + Xamarin.Forms working, with no C# actually in use. It's working fine, but now I'm trying to create a BindableProperty on a Control that I'm creating. It kindof works, but when I try to bind to it in XAML with a {DynamicResource blah} or in a Style with then it all falls apart.

Both working:

<dashboard:ProgressRing DotOnColor="#00d4c3" DotOffColor="#120a22" />
<dashboard:ProgressRing DotOnColor="{StaticResource dotOnColor}" DotOffColor="{StaticResource dotOffColor}" />

Not working:

<dashboard:ProgressRing DotOnColor="{DynamicResource dotOnColor}" DotOffColor="{DynamicResource dotOffColor}" />

Error:

Xamarin.Forms.Xaml.XamlParseException: Position 18:29. Cannot assign property "DotOnColor": Property does not exists, or is not assignable, or mismatching type between value and property

XAML:

<?xml version="1.0" encoding="UTF-8"?>
<ContentView
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="Dashboard.ProgressRing"
    x:Name="view">
  <AbsoluteLayout x:Name="absLayout">
    <!-- Dots are controlled in the code behind -->
  </AbsoluteLayout>
</ContentView>

Code behind:

namespace Dashboard

open System
open Xamarin.Forms
open Xamarin.Forms.Xaml

type ProgressRing() =
    inherit ContentView()

    do base.LoadFromXaml(typeof<ProgressRing>) |> ignore
    let absLayout = base.FindByName<AbsoluteLayout>("absLayout")

    static let dotOffColorProperty = BindableProperty.Create("DotOffColor", typeof<Color>, typeof<ProgressRing>, Color.Default)
    static let dotOnColorProperty = BindableProperty.Create("DotOnColor", typeof<Color>, typeof<ProgressRing>, Color.Accent)

    static member DotOffColorProperty = dotOffColorProperty
    static member DotOnColorProperty = dotOnColorProperty

    member this.DotOffColor
        with get () = this.GetValue dotOffColorProperty :?> Color
        and set (value:Color) =
            this.SetValue(dotOffColorProperty, value)
    member this.DotOnColor
        with get () = this.GetValue dotOnColorProperty :?> Color
        and set (value:Color) =
            this.SetValue(dotOnColorProperty, value)

I believe the cause of this is the static member failing - it's a public static property, where Xamarin.Forms expects a public static field.

F# officially does not do public static fields, which causes problems in situations like this - see some discussion here: http://www.ianvoyce.com/index.php/2010/10/01/public-static-fields-gone-from-f-2-0/

like image 365
Lamarth Avatar asked Oct 29 '22 07:10

Lamarth


1 Answers

Xamarin.Forms is definitely looking for a field - I found the following in https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs:

    static BindableProperty GetBindableProperty(Type elementType, string localName, IXmlLineInfo lineInfo,
        bool throwOnError = false)
    {
        var bindableFieldInfo =
            elementType.GetFields().FirstOrDefault(fi => fi.Name == localName + "Property" && fi.IsStatic && fi.IsPublic);

        Exception exception = null;
        if (exception == null && bindableFieldInfo == null) {
            exception =
                new XamlParseException(
                    string.Format("BindableProperty {0} not found on {1}", localName + "Property", elementType.Name), lineInfo);
        }

        if (exception == null)
            return bindableFieldInfo.GetValue(null) as BindableProperty;
        if (throwOnError)
            throw exception;
        return null;
    }

Guess I'll try to fix it to handle public static Properties too. After I do that, any tips on how to submit?

like image 192
Lamarth Avatar answered Jan 02 '23 20:01

Lamarth