我有F#+ Xamarin.Forms工作,实际上没有使用C#。它工作正常,但现在我试图在我正在创建的控件上创建一个BindableProperty。它很有用,但是当我尝试使用{DynamicResource blah}在XAML中绑定它或者在样式中绑定它时,它就会崩溃。
两者都在工作:
<dashboard:ProgressRing DotOnColor="#00d4c3" DotOffColor="#120a22" />
<dashboard:ProgressRing DotOnColor="{StaticResource dotOnColor}" DotOffColor="{StaticResource dotOffColor}" />
不工作:
<dashboard:ProgressRing DotOnColor="{DynamicResource dotOnColor}" DotOffColor="{DynamicResource dotOffColor}" />
错误:
Xamarin.Forms.Xaml.XamlParseException:位置18:29。无法分配属性&#34; DotOnColor&#34;:属性不存在,或者不可分配,或者值和属性之间的类型不匹配
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>
代码背后:
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)
我认为原因是静态成员失败 - 它是一个公共静态属性,其中Xamarin.Forms需要一个公共的静态字段。
F#官方不会执行公共静态字段,这会在这种情况下导致问题 - 请参阅此处的一些讨论: http://www.ianvoyce.com/index.php/2010/10/01/public-static-fields-gone-from-f-2-0/
答案 0 :(得分:2)
Xamarin.Forms肯定在寻找一个领域 - 我在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;
}
猜猜我会尝试修复它来处理公共静态属性。在我这样做之后,有关如何提交的提示吗?