重要的事情说三遍:
本文基本是取自微软官方 Bindable Properties, 官方也提供了机翻的中文版本,笔者只是尝试用自己的理解描述一遍,便于记忆。如有不对之处,欢迎拍砖。
本文基本是取自微软官方 Bindable Properties, 官方也提供了机翻的中文版本,笔者只是尝试用自己的理解描述一遍,便于记忆。如有不对之处,欢迎拍砖。
本文基本是取自微软官方 Bindable Properties, 官方也提供了机翻的中文版本,笔者只是尝试用自己的理解描述一遍,便于记忆。如有不对之处,欢迎拍砖。
概述
首先来说说Bindableproperty是什么?官方的说是:在Xamarin.Forms中,公共语言运行时(CLR)的属性(properites)的功能由bindable properties来扩展。它是一种特殊的属性,特殊的点在于它的值是被Xamarin.Foms的属性系统跟踪的。
它是通过使用备份一个BindableProperty的类型来实现扩展property的功能的,而不是使用备份field的方式。它的目标是提供这样一个属性系统,这个系统支持类似于父子关系的方式来设置数据绑定,样式更改等等。此外,它还额外提供了一些别的功能,如设置默认值,验证属性合法性,提供属性更改时的回调函数等等。
上面两段内容确实很绕,换成笔者的理解是:利用BindableProperty来创建的属性不同于我们平常所使用的形如 public string abc { get; set; } 这样的普通属性,它额外提供了对数据绑定,以及相关方法的支持。这种功能是通过Xamrin.Forms这个框架自身对bindable properties的值的追踪来实现的。
如果你有下面的某些功能的需求,那么你就应该使用bindable properties
举个例子,Xamarin.Forms自带的如 Label.Text,Button.BorderRadius,StackLayout.Orientation等等都是可绑定属性。任何一个可绑定的属性在其所在的class内都有一个由pubilc static readonly property修饰的BindableProperty对应。加粗的这句话不是很好理解,举个例子。如前面提到的Xamarin.Forms自带的Lable.Text是一个可绑定属性,那么在Lable这个类中也一定包含这样的定义
public static readonly Xamarin.Forms.BindableProperty TextProperty;
创建和实用可绑定属性
可以使用如下的流程来创建可绑定属性:
请注意,所有的可绑定属性都必须在UI Thread上创建。这意味着只有跑在UI Thread上的代码可以获取(get)或改变(set)可绑定属性的值。然而,可以通过使用Device.BeginInvokeOnMainThread方法将可绑定属性的实例封送到UI Thread,来实现从其他线程访问。(这句的翻译不是很准确,原文是:However, BindableProperty
instances can be accessed from other threads by marshaling to the UI thread with the Device.BeginInvokeOnMainThread method.)
创建
包含BindableProperty属性的类必须派生自BindableObject,但BindableObject继承关系中处于较高的位置,因此大部分用于UI的类都支持可绑定属性。
通过使用public static readonly来声明一个可绑定属性,使用BindableProperty.Create的某个重载来创建它。此处要注意可绑定属性应该定义在BindableObject派生类的主体内,并且在该类任何成员的外部。
创建可绑定属性时至少应该指定下面所需的参数:
例如:
public static readonly BindableProperty EventNameProperty = BindableProperty.Create ("EventName", typeof(string), typeof(EventToCommandBehavior), null);
这段代码将创建一个名为“EventName”,string类型的可绑定属性的实例。它属于EventToCommandBehavior这个类,并且默认值是null。可绑定属性的命名规范要求其必须与Create方法中所指定的name相匹配,并在后面追加"Property"。如上面的代码,EventName与EventNameProperty。
此外,在创建BindableProperty的实例时,还可以指定以下的一些参数
创建属性访问器
需要使用属性语法(property syntax)来创建可用于访问 可绑定属性 的属性访问器。Get方法需要利用GetValue从对应的BindableProperty中获取值,再使用类型转换拿到所需的值。Set方法需要使用SetValue方法来为其复制。例如
public string EventName { get { return (string)GetValue (EventNameProperty); } set { SetValue (EventNameProperty, value); } }
使用可绑定属性
一旦声明了可绑定属性就可以在XAML和C#代码中使用它。在XAML中,还需要先声明带有前缀的命名空间,这个空间中包含着我们创建的可绑定属性所属的类。在XAML中
声明
<ContentPage ... xmlns:local="clr-namespace:EventToCommandBehavior" ...> ... </ContentPage>
使用
<ListView ...> <ListView.Behaviors> <local:EventToCommandBehavior EventName="ItemSelected" ... /> </ListView.Behaviors> </ListView>
等效的C#代码
var listView = new ListView (); listView.Behaviors.Add (new EventToCommandBehavior { EventName = "ItemSelected", ... });
进阶使用场景
检测属性更改
在利用BindableProperety.Create方法创建可绑定属性的实例时可以通过propertyChanged参数来注册一个静态的回调方法,该方法会在可绑定属性的值发生变更时执行。例如:
public static readonly BindableProperty EventNameProperty = BindableProperty.Create ( "EventName", typeof(string), typeof(EventToCommandBehavior), null, propertyChanged: OnEventNameChanged); ... static void OnEventNameChanged (BindableObject bindable, object oldValue, object newValue) { // Property changed implementation goes here }
OnEventNameChanged方法中的参数BindableObject bindable用于指定是哪个实例在执行这个方法,后面两个参数就不解释了。
用于验证属性的回调函数
注册方法与上面类似,直接贴代码
public static readonly BindableProperty AngleProperty = BindableProperty.Create ("Angle", typeof(double), typeof(HomePage), 0.0, validateValue: IsValidValue); ... static bool IsValidValue (BindableObject view, object value) { double result; bool isDouble = double.TryParse (value.ToString (), out result); return (result >= 0 && result <= 360); }
验证属性的这个方法会根据接收到的参数的值以及你定义好的逻辑来判断它是否通过验证。如果没有通过验证则返回false,开发需要处理这种情况。(如ui上style的变更,以提示用户输入值不满足要求等等)。如上面代码中的例子,被验证的值只有在处于[0,360]才能验证通过。
强制值回调(Coerce Value Callback)
注册方法与上面类似,直接贴代码
public static readonly BindableProperty AngleProperty = BindableProperty.Create ( "Angle", typeof(double), typeof(HomePage), 0.0, coerceValue: CoerceAngle); public static readonly BindableProperty MaximumAngleProperty = BindableProperty.Create ( "MaximumAngle", typeof(double), typeof(HomePage), 360.0); ... static object CoerceAngle (BindableObject bindable, object value) { var homePage = bindable as HomePage; double input = (double)value; if (input > homePage.MaximumAngle) { input = homePage.MaximumAngle; } return input; }
这里需要说明一下。coerceValue方法会在属性值发生变更时调用。它用于在属性值变更时重新改变属性的值。如上面的代码所示,可以使用它来确保一个可绑定属性的值不大于另一个可绑定属性的值。该CoerceAngle
方法检查MaximumAngle
属性的值,如果Angle
属性值大于它,则它会将值强制转换为MaximumAngle
属性值。
使用Func创建默认值
贴代码
public static readonly BindableProperty SizeProperty = BindableProperty.Create ("Size", typeof(double), typeof(HomePage), 0.0, defaultValueCreator: bindable => Device.GetNamedSize (NamedSize.Large, (Label)bindable));
此处使用一个Device.GetNamedSize方法来获取在原生平台(native platform)上lable的字体大小,并将其设置为SizeProperty的默认值。
Xamarin Bindableproperty 可绑定属性
原文:https://www.cnblogs.com/dogtwo0214/p/11461010.html