Language

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 デザイン パターンの View と View Model の間など、変更に応じてアプリケーション開発者がオブジェクト間でデータを同期できるようにするデータ ストレージ メカニズムを提供します。 Xamarin.Forms名前空間のすべてのビジュアル要素は、BindableObject クラスから継承されるため、ユーザー インターフェイス要素の背後にあるデータを、アプリケーション開発者が提供するビュー モデルにバインドするために使用できます。

BindableObject (通常はビュー) のプロパティの背後にあるデータをビュー モデルのプロパティにバインドするには、アプリケーション開発者は次の操作を行う必要があります。

最初に、開発者はビューにプロパティのペアを作成します。そのうちの 1 つは BindablePropertyで、もう 1 つは必要な型のプロパティです。 次のコードでは、 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));
    }
}

3 番目に、最後に、アプリケーション開発者は BindableObject のインスタンスを、INotifyPropertyChanged を実装するインスタンスにバインドします。 MVVM デザイン パターンのボキャブラリでは、これは "View のインスタンスをビュー モデルのインスタンスにバインドする" です。 この手順が完了すると、データの変更は、バインド 手順中に渡された BindingMode 列挙の値 (存在する場合) によって決定される方法で、ビュー モデルとビュー モデルの間で伝達されます。

以下のコードは、上記のクラスを参照するプロジェクトに含まれている場合、 MockBindable と MockViewModelの両方のインスタンスを作成し、初期化を実行し、バインディングを設定してから、一方向のバインドを示します。 次のコードは、例外をスローせずに実行されます。

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)

propertyのSetValueによって設定されたすべての値をクリアします。

ClearValue(BindablePropertyKey)

propertyKeyによって識別されるプロパティのSetValueによって設定されたすべての値をクリアします。

CoerceValue(BindableProperty)

アプリケーション開発者が検証、型強制、およびイベント システムを有効にすることで、あるオブジェクトのデータに加えられた変更を別のオブジェクトに伝達できるメカニズムを提供します。 BindableProperty。

CoerceValue(BindablePropertyKey)

アプリケーション開発者が検証、型強制、およびイベント システムを有効にすることで、あるオブジェクトのデータに加えられた変更を別のオブジェクトに伝達できるメカニズムを提供します。 BindableProperty。

GetValue(BindableProperty)

BindableProperty に含まれる値を返します。

GetValues(BindableProperty, BindableProperty, BindableProperty)
古い.

Xamarinによる内部使用の場合。フォーム プラットフォーム。

GetValues(BindableProperty, BindableProperty)
古い.

Xamarinによる内部使用の場合。フォーム プラットフォーム。

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による内部使用の場合。フォーム プラットフォーム。

UnapplyBindings()

以前に設定したすべてのバインディングを適用解除します。

イベント

名前 説明
BindingContextChanged

BindingContext プロパティが変更されるたびに発生します。

PropertyChanged

プロパティが変更されたときに発生します。

PropertyChanging

プロパティが変更されようとしているときに発生します。

明示的なインターフェイスの実装

名前 説明
IDynamicResourceHandler.SetDynamicResource(BindableProperty, String)

Xamarinによる内部使用の場合。フォーム プラットフォーム。

拡張メソッド

名前 説明
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。

適用対象