Udostępnij przez


BindableObjectExtensions.SetBinding Metoda

Definicja

Przeciążenia

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

Tworzy i stosuje powiązanie do właściwości.

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

Parametry

targetProperty
BindableProperty

Właściwość BindableProperty, na której ma być ustawione powiązanie.

path
System.String

Wartość System.String wskazująca ścieżkę właściwości, z którą ma być powiązana.

mode
BindingMode

Element BindingMode dla powiązania. Ten parametr jest opcjonalny. Wartość domyślna to Default.

converter
IValueConverter

Element IValueConverter dla powiązania. Ten parametr jest opcjonalny. Wartość domyślna to null.

stringFormat
System.String

Ciąg używany jako stringFormat dla powiązania. Ten parametr jest opcjonalny. Wartość domyślna to null.

Uwagi

W poniższym przykładzie pokazano, jak za pomocą metody rozszerzenia ustawić powiązanie.

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"

Dotyczy

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

Przestroga

Ten interfejs API jest już przestarzały.

Tworzy i stosuje powiązanie na podstawie wyrażenia.

[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

Parametry typu

TSource

Typ źródła.

Parametry

self
BindableObject

The BindableObject.

targetProperty
BindableProperty

Właściwość BindableProperty do powiązania z

sourceProperty
System.Linq.Expressions.Expression<System.Func<TSource,System.Object>>

Wyrażenie używane do pobierania ścieżki źródłowej.

mode
BindingMode

Tryb BindingMode dla powiązania. Ten parametr jest opcjonalny. Wartość domyślna to Default.

converter
IValueConverter

Element IValueConverter dla powiązania. Ten parametr jest opcjonalny. Wartość domyślna to null.

stringFormat
System.String

Ciąg używany jako stringFormat dla powiązania. Ten parametr jest opcjonalny. Wartość domyślna to null.

Atrybuty
System.ComponentModel.EditorBrowsableAttribute System.ObsoleteAttribute

Uwagi

Ta metoda rozszerzenia używa wyrażenia zamiast ścieżki do tworzenia i ustawiania powiązań. Używanie wyrażeń jest bardziej przyjazne dla refaktoryzacji.

W poniższym przykładzie pokazano ustawienie powiązania przy użyciu metody rozszerzenia.

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"

Dotyczy