Binding 构造函数

定义

重载

Binding()

构造并初始化 Binding 类的新实例。

Binding(String, BindingMode, IValueConverter, Object, String, Object)

构造并初始化 Binding 类的新实例。

Binding()

构造并初始化 Binding 类的新实例。

public Binding ();

适用于

Binding(String, BindingMode, IValueConverter, Object, String, Object)

构造并初始化 Binding 类的新实例。

public Binding (string path, Xamarin.Forms.BindingMode mode = Xamarin.Forms.BindingMode.Default, Xamarin.Forms.IValueConverter converter = default, object converterParameter = default, string stringFormat = default, object source = default);
new Xamarin.Forms.Binding : string * Xamarin.Forms.BindingMode * Xamarin.Forms.IValueConverter * obj * string * obj -> Xamarin.Forms.Binding

参数

path
System.String

属性路径。

mode
BindingMode

绑定模式。 此属性是可选的。 默认为 Default

converter
IValueConverter

转换器。 此参数可选。 默认值为 null

converterParameter
System.Object

要传递到转换器的用户定义参数。 此参数可选。 默认值为 null

stringFormat
System.String

字符串格式。 此参数可选。 默认值为 null

source
System.Object

用作绑定源的对象。 此参数可选。 默认值为 null

注解

以下示例演示如何使用 BindingMode 和 Converter 将绑定设置为属性:

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

public class ReverseConverter : IValueConverter
{
  public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    var s = value as string;
    if (s == null)
      return value;
    return new string (s.Reverse ().ToArray ());
  }

  public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    var s = value as string;
    if (s == null)
      return value;
    return new string (s.Reverse ().ToArray ());
  }
}

var label = new Label ();
PersonViewModel person;
label.BindingContext = person = new PersonViewModel { Name = "John Doe", Company= "Xamarin" };
label.SetBinding (Label.TextProperty, new Binding ("Name", mode: BindingMode.TwoWay, converter: new ReverseConverter ()));
Debug.WriteLine (label.Text); //prints "eoD nhoJ". ReverseConverter.Convert () is invoked in this case.

label.Text = "ooF";
Debug.WriteLine (person.Name); //prints "Foo". ReverseConverter.ConvertBack () is invoked in this case. The label Text change is propagated back as the BindingMode is TwoWay.

适用于