每个可绑定属性都有一个对应public static readonly
的类型字段,该字段BindableProperty
在同一类上公开,并且是可绑定属性的标识符。
您可以检查提供 binable 属性的控件的源代码。
例如,我使用内容视图。代码来自下面的链接。Xamarin 表单从可绑定属性更新视图模型字段
public partial class MyCustomControl : ContentView
{
private string _text;
public string Text
{
get { return _text; }
set
{
_text = value;
OnPropertyChanged();
}
}
public static readonly BindableProperty TextProperty = BindableProperty.Create(
nameof(Text),
typeof(string),
typeof(MyCustomControl),
string.Empty,
propertyChanged: (bindable, oldValue, newValue) =>
{
var control = bindable as MyCustomControl;
//var changingFrom = oldValue as string;
//var changingTo = newValue as string;
control.Title.Text = newValue.ToString();
});
public MyCustomControl()
{
InitializeComponent();
}
提供ContentView
公共静态只读 BindableProperty。
//
// Summary:
// An element that contains a single child element.
//
// Remarks:
// The following example shows how to construct a new ContentView with a Label inside.
[ContentProperty("Content")]
public class ContentView : TemplatedView
{
//
// Summary:
// Backing store for the Xamarin.Forms.ContentView.Content property..
//
// Remarks:
// To be added.
public static readonly BindableProperty ContentProperty;
public ContentView();
//
// Summary:
// Gets or sets the content of the ContentView.
public View Content { get; set; }
//
// Summary:
// Method that is called when the binding context changes.
//
// Remarks:
// To be added.
protected override void OnBindingContextChanged();
}
您可以查看 MS 文档以了解更多关于可二进制属性中的静态的信息。https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/bindable-properties