BindableObjectExtensions.SetBinding 方法

定义

重载

SetBinding(BindableObject, BindableProperty, String, BindingMode, IValueConverter, String)

创建绑定并将其应用到属性。

SetBinding<TSource>(BindableObject, BindableProperty, Expression<Func<TSource,Object>>, BindingMode, IValueConverter, String)
已过时。

通过表达式创建并应用绑定。

SetBinding(BindableObject, BindableProperty, String, BindingMode, IValueConverter, String)

创建绑定并将其应用到属性。

public static void SetBinding (this Xamarin.Forms.BindableObject self, Xamarin.Forms.BindableProperty targetProperty, string path, Xamarin.Forms.BindingMode mode = Xamarin.Forms.BindingMode.Default, Xamarin.Forms.IValueConverter converter = default, string stringFormat = default);
static member SetBinding : Xamarin.Forms.BindableObject * Xamarin.Forms.BindableProperty * string * Xamarin.Forms.BindingMode * Xamarin.Forms.IValueConverter * string -> unit

参数

targetProperty
BindableProperty

要在其上设置绑定的 BindableProperty。

path
String

一个 String,指示要绑定到的属性路径。

mode
BindingMode

该绑定的 BindingMode。 此参数可选。 默认值为 Default

converter
IValueConverter

此绑定的 IValueConverter。 此参数可选。 默认值为 null

stringFormat
String

用作绑定的 stringFormat 的字符串。 此参数可选。 默认值为 null

注解

以下示例演示如何使用 扩展方法设置绑定。

public class PersonViewModel
{
    public string Name { get; set; }
    public string Company { get; set; }
}

// ...

var vm = new PersonViewModel {
    Name = "John Doe", 
    Company = "Xamarin"
}

var label = new Label ();
label.SetBinding (Label.TextProperty, "Name"); // "Name" is the property on the view model
label.BindingContext = vm;

Debug.WriteLine (label.Text); // prints "John Doe"

适用于

SetBinding<TSource>(BindableObject, BindableProperty, Expression<Func<TSource,Object>>, BindingMode, IValueConverter, String)

注意

现已弃用此 API。

通过表达式创建并应用绑定。

[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
[System.Obsolete]
public static void SetBinding<TSource> (this Xamarin.Forms.BindableObject self, Xamarin.Forms.BindableProperty targetProperty, System.Linq.Expressions.Expression<Func<TSource,object>> sourceProperty, Xamarin.Forms.BindingMode mode = Xamarin.Forms.BindingMode.Default, Xamarin.Forms.IValueConverter converter = default, string stringFormat = default);
static member SetBinding : Xamarin.Forms.BindableObject * Xamarin.Forms.BindableProperty * System.Linq.Expressions.Expression<Func<'Source, obj>> * Xamarin.Forms.BindingMode * Xamarin.Forms.IValueConverter * string -> unit

类型参数

TSource

源类型。

参数

self
BindableObject

BindableObject。

targetProperty
BindableProperty

要绑定到的 BindableProperty

sourceProperty
Expression<Func<TSource,Object>>

用于检索源路径的表达式。

mode
BindingMode

绑定的 BindingMode。 此参数可选。 默认值为 Default

converter
IValueConverter

绑定的 IValueConverter。 此参数可选。 默认值为 null

stringFormat
String

用作绑定的 stringFormat 的字符串。 此参数可选。 默认值为 null

属性

注解

此扩展方法使用 Expression 而不是 path 来创建和设置绑定。 使用表达式更便于重构。

以下示例演示了使用 扩展方法的绑定的设置。

public class PersonViewModel
{
    public string Name { get; set; }
    public string Company { get; set; }
}

// ...

var vm = new PersonViewModel {
    Name = "John Doe", 
    Company = "Xamarin"
};

var label = new Label ();
label.SetBinding<PersonViewModel> (Label.TextProperty, vm => vm.Name);  // "Name" is the property on the view model
label.BindingContext = vm;

Debug.WriteLine (label.Text); // prints "John Doe"

适用于