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 継承されるため、ユーザー インターフェイス要素の背後にあるデータを、アプリケーション開発者が提供する View Models にバインドするために使用できます。

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

最初に、開発者はビューにプロパティのペアを作成します。そのうち BindablePropertyの 1 つは です。もう 1 つは、必要な型のプロパティです。 次のコードでは、 MockBindableObject は、通常、運用コードのユーザー インターフェイス オブジェクトを表しています。 アプリケーション開発者は、 と GetValue(BindableProperty) を使用SetValue(BindableProperty, Object)して、バインドされたプロパティの値を取得および設定する必要があります。目的の型の プロパティは、バインドされたプロパティのターゲットが実装するインターフェイスを提供します。

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

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)

propertySetValue によって設定された値を消去します。

ClearValue(BindablePropertyKey)

propertyKey によって識別されるプロパティの SetValue によって設定される値を消去します。

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.

適用対象