BindableObject.SetValue 方法

定义

重载

SetValue(BindableProperty, Object)

设置指定属性的值。

SetValue(BindablePropertyKey, Object)

设置 propertyKey 的值。

SetValue(BindableProperty, Object)

设置指定属性的值。

public void SetValue (Xamarin.Forms.BindableProperty property, object value);
member this.SetValue : Xamarin.Forms.BindableProperty * obj -> unit

参数

property
BindableProperty

要在其上分配值的 BindableProperty。

value
Object

要设置的值。

注解

GetValue(BindableProperty)SetValue 用于访问由实现的属性的值 BindableProperty 。 也就是说,应用程序开发人员通常通过定义 public 属性来为绑定属性提供接口,该属性的访问 get 器将 的结果 GetValue(BindableProperty) 强制转换为适当的类型并返回该属性,其 set 访问器使用 SetValue 在正确的属性上设置值。 应用程序开发人员不应在定义绑定属性接口的公共属性中执行其他步骤。

以下示例演示如何为在运行时进行绑定时在目标属性中提供的实现创建可绑定属性接口。

class MyBindable : BindableObject
{
    public static readonly BindableProperty MyProperty = 
      BindableProperty.Create<MyBindable, string> (w => w.My, default(string));

    public string My {
      get { return (string)GetValue (MyProperty); }
      set { SetValue (MyProperty, value); } 
    }
}

适用于

SetValue(BindablePropertyKey, Object)

设置 propertyKey 的值。

public void SetValue (Xamarin.Forms.BindablePropertyKey propertyKey, object value);
member this.SetValue : Xamarin.Forms.BindablePropertyKey * obj -> unit

参数

propertyKey
BindablePropertyKey

要在其上分配值的 BindablePropertyKey。

value
Object

要设置的值。

注解

此方法 和 BindablePropertyKey 可用于在有限的写入访问权限下实现 BindableProperties。 写入访问权限仅限于 BindablePropertyKey 的范围。

以下示例演示如何声明具有“内部”写入访问权限的 BindableProperty。

class MyBindable : BindableObject
{
  internal static readonly BindablePropertyKey MyPropertyKey = 
    BindableProperty.CreateReadOnly<MyBindable, string> (w => w.My, default(string));
  public static readonly BindableProperty MyProperty = MyPropertyKey.BindableProperty;

  public string My {
    get { return (string)GetValue (MyProperty); }
    internal set { SetValue (MyPropertyKey, value); } 
  }
}

适用于