BindableObject 类

定义

通过启用验证、类型强制转换和事件系统,提供一种机制,应用程序开发者可以通过该机制将对一个对象中的数据所做的更改传播到另一个对象。 BindableProperty.

public abstract class BindableObject : System.ComponentModel.INotifyPropertyChanged, Xamarin.Forms.Internals.IDynamicResourceHandler
type BindableObject = class
    interface INotifyPropertyChanged
    interface IDynamicResourceHandler
继承
BindableObject
派生
实现

注解

BindableObject 提供了一种数据存储机制,使应用程序开发人员能够在对象之间同步数据以响应更改,例如,在 MVVM 设计模式中的视图模型与视图模型之间。 命名空间中的所有 Xamarin.Forms 视觉元素都继承自 BindableObject 类,因此它们都可用于将用户界面元素背后的数据绑定到应用程序开发人员提供的视图模型。

若要将 (通常是视图)中的 BindableObject属性背后的数据绑定到视图模型中的属性,应用程序开发人员应执行以下操作。

首先,开发人员在视图上创建一对属性,其中一个 BindableProperty是 ,另一个是所需的任何类型的属性。 在下面的代码中, MockBindableObject 表示生产代码中通常为用户界面对象的内容。 应用程序开发人员应注意使用 SetValue(BindableProperty, Object)GetValue(BindableProperty) 来获取和设置绑定属性的值;所需类型的 属性提供绑定属性的目标将实现的接口。

class MockBindableObject : BindableObject
{
    // App developers should use the method below in production code for 
    // better performance
    public static readonly BindableProperty BoundNameProperty =
         BindableProperty.Create ("Foo", typeof (string),
                                  typeof (MockBindableObject),
                                  default(string));

    // App developers should use the method below during development for
    // design-time error checking as the codebase evolves.
    // public static readonly BindableProperty FooProperty 
    //     = BindableProperty.Create<MockBindableObject, string> (
    //         o => o.Foo, default (string)
    //     );

    public string BoundName
    {
        get { return (string) GetValue (BoundNameProperty); }
        set { SetValue (BoundNameProperty, value); }
    }
}

其次,开发人员在实现 接口的类中为绑定属性创建 实现 INotifyPropertyChanged 。 在 MVVM 设计模式中,这通常由视图模型完成。 应用程序开发人员应在要用作视图模型的类上实现 INotifyPropertyChanged 接口。 在下面的示例中,应用开发人员应记下实现属性 Name 的惯用方式,首先,确保属性实际更改并在未更改时返回 ,然后才分配值并调用 OnPropertyChanged(String) 方法。 此外, Name 以下示例中的 属性仅包装 name 字段。 实际上,应用程序开发人员可以选择不同的模型来存储应用程序数据。

class MockViewModel : INotifyPropertyChanged
{
    string name;

    public string Name
    {
        get { return name; }
        set
        {
            // OnPropertyChanged should not be called if the property value
            // does not change.
            if (name == value)
                return;
            name = value;
            OnPropertyChanged ();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    void OnPropertyChanged (string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler (this, new PropertyChangedEventArgs (propertyName));
    }
}

第三,最后,应用程序开发人员将 BindableObject 的实例绑定到实现 INotifyPropertyChanged 的实例。 在 MVVM 设计模式的词汇中,这是“将视图实例绑定到视图模型的实例”。 完成此步骤后,数据更改将按照绑定步骤中传递的 BindingMode 枚举值(如果有)确定的方式在视图模型和视图模型之间传播。

下面的代码包含在引用上述类的项目中时,会创建 和 MockViewModelMockBindable实例,执行一些初始化,设置绑定,然后演示单向绑定。 以下代码在运行时不会引发异常。

public static void OneWayDemo ()
{
    var view = new MockBindableObject ();
    var viewModel = new MockViewModel ();

    // Pre-load the ViewModel, for demonstration purposes
    viewModel.Name = "Testing";

    // Create a one-way (default) binding
    view.SetBinding (MockBindableObject.BoundNameProperty, new Binding ("Name"));

    // App developers should only set the binding context after all
    // calls to SetBinding() have been made, for performance reasons.
    view.BindingContext = viewModel;

    // In a one way binding, the ViewModel value will be used to update
    // the values in the View during initialization
    if (view.BoundName != "Testing")
        throw new Exception ();

    view.BoundName = "gnitseT";

    // in a one way binding, changes to the View will NOT update the ViewModel
    if (viewModel.Name == "gnitseT")
        throw new Exception ();
}

构造函数

BindableObject()

初始化 BindableObject 类的新实例。

字段

BindingContextProperty

实现其接口由 BindingContext 属性提供的绑定属性。

属性

BindingContext

获取或设置对象,该对象包含将被属于此 BindableObject 的绑定属性设定为目标的属性。

Dispatcher

通过启用验证、类型强制转换和事件系统,提供一种机制,应用程序开发者可以通过该机制将对一个对象中的数据所做的更改传播到另一个对象。 BindableProperty.

方法

ApplyBindings()

将绑定应用到 BindingContext

ClearValue(BindableProperty)

清除由 SetValueproperty 设置的任何值。

ClearValue(BindablePropertyKey)

清除由 SetValuepropertyKey 标识的属性设置的任何值。

CoerceValue(BindableProperty)

通过启用验证、类型强制转换和事件系统,提供一种机制,应用程序开发者可以通过该机制将对一个对象中的数据所做的更改传播到另一个对象。 BindableProperty.

CoerceValue(BindablePropertyKey)

通过启用验证、类型强制转换和事件系统,提供一种机制,应用程序开发者可以通过该机制将对一个对象中的数据所做的更改传播到另一个对象。 BindableProperty.

GetValue(BindableProperty)

返回 BindableProperty 中包含的值。

GetValues(BindableProperty, BindableProperty)
已过时。

供 Xamarin.Forms 平台内部使用。

GetValues(BindableProperty, BindableProperty, BindableProperty)
已过时。

供 Xamarin.Forms 平台内部使用。

IsSet(BindableProperty)

如果目标属性存在并且已设置,则返回 true

OnBindingContextChanged()

替代此方法以在 BindingContext 更改时执行操作。

OnPropertyChanged(String)

从子类调用此方法以通知属性发生更改。

OnPropertyChanging(String)

从子类调用此方法以通知属性将要发生更改。

RemoveBinding(BindableProperty)

删除先前设置的绑定。

SetBinding(BindableProperty, BindingBase)

向属性分配绑定。

SetInheritedBindingContext(BindableObject, Object)

将继承的上下文设置为嵌套元素。

SetValue(BindableProperty, Object)

设置指定属性的值。

SetValue(BindablePropertyKey, Object)

设置 propertyKey 的值。

SetValueCore(BindableProperty, Object, SetValueFlags)

供 Xamarin.Forms 平台内部使用。

UnapplyBindings()

不应用所有以前设置的绑定。

事件

BindingContextChanged

只要 BindingContext 属性更改就会引发。

PropertyChanged

在属性已更改时引发。

PropertyChanging

在属性将要更改时引发。

显式接口实现

IDynamicResourceHandler.SetDynamicResource(BindableProperty, String)

供 Xamarin.Forms 平台内部使用。

扩展方法

GetPropertyIfSet<T>(BindableObject, BindableProperty, T)

通过启用验证、类型强制转换和事件系统,提供一种机制,应用程序开发者可以通过该机制将对一个对象中的数据所做的更改传播到另一个对象。 BindableProperty.

SetAppThemeColor(BindableObject, BindableProperty, Color, Color)

通过启用验证、类型强制转换和事件系统,提供一种机制,应用程序开发者可以通过该机制将对一个对象中的数据所做的更改传播到另一个对象。 BindableProperty.

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

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

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

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

SetOnAppTheme<T>(BindableObject, BindableProperty, T, T)

通过启用验证、类型强制转换和事件系统,提供一种机制,应用程序开发者可以通过该机制将对一个对象中的数据所做的更改传播到另一个对象。 BindableProperty.

适用于