在代码中绑定可绑定的附加属性。
首先需要定义可绑定的附加属性,例如:
public static readonly BindableProperty MyProperty = BindableProperty.CreateAttached("MyProperty", typeof(string), typeof(MyClass), defaultValue: "");
public static string GetMyProperty(BindableObject target)
{
return (string)target.GetValue(MyProperty);
}
public static void SetMyProperty(BindableObject target, string value)
{
target.SetValue(MyProperty, value);
}
然后,在代码中使用SetValue方法来绑定属性,例如:
myLabel.SetValue(MyClass.MyProperty, "hello world");
最后,使用GetValue方法来获取属性值,例如:
string value = (string)myLabel.GetValue(MyClass.MyProperty);