BindableObject 類別

定義

提供一個機制,應用程式開發人員可以利用此機制,透過啟用驗證、類型強制型轉和事件系統,將某個物件中的資料變更傳播到另一個物件。 BindableProperty.

public abstract class BindableObject : System.ComponentModel.INotifyPropertyChanged, Xamarin.Forms.Internals.IDynamicResourceHandler
type BindableObject = class
    interface INotifyPropertyChanged
    interface IDynamicResourceHandler
繼承
System.Object
BindableObject
衍生
實作
System.ComponentModel.INotifyPropertyChanged IDynamicResourceHandler

備註

類別 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); }
    }
}

其次,開發人員會在實作 介面的類別中建立系結屬性的實作 System.ComponentModel.INotifyPropertyChanged 。 在MVVM設計模式中,這通常是由檢視模型完成。 應用程式開發人員應該在想要作為檢視模型的類別上實 System.ComponentModel.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)

清除 SetValue 針對 property 所設定的任何值。

ClearValue(BindablePropertyKey)

清除 SetValue 針對 propertyKey 所識別屬性設定的任何值。

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.

適用於