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

I have F # + Xamarin.Forms working without using C #. It works fine, but now I am trying to create a BindableProperty for the control I am creating. It works, but when I try to bind it to XAML with {DynamicResource blah} or styled then it all falls apart.

Both workers:

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

      

Does not work:

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

      

Mistake:

Xamarin.Forms.Xaml.XamlParseException: Position 18:29. Cannot assign property "DotOnColor": property does not exist or cannot be assigned or does not match the 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 reason for this is the failure of the static member - it is a public static property where Xamarin.Forms expects a public static field.

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

+3


source to share


1 answer


Xamarin.Forms is definitely looking for a field - I found the following at 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;
    }

      



Suppose I try to fix this to handle public static properties. After I do this, any tips on how to submit?

0


source







All Articles