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,而不是路徑來建立和設定系結。 使用運算式更容易重構。

下列範例說明使用擴充方法的系結設定。

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"

適用於