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 アプリケーション開発者が次の操作を行う必要があります。

最初に、開発者はビューにプロパティのペアを作成します。1つはで、もう1つは、 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 ます。

次のコードは、上記のクラスを参照するプロジェクトに含まれている場合、 MockBindableMockViewModelの両方のインスタンスを作成し、何らかの処理を実行し、バインディングを設定して、一方向のバインドを示します。 次のコードは、例外をスローすることなく実行されます。

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

メソッド

ApplyBindings()

BindingContext にバインディングを適用します。

ClearValue(BindableProperty)

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

ClearValue(BindablePropertyKey)

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

CoerceValue(BindableProperty)
CoerceValue(BindablePropertyKey)
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)
SetAppThemeColor(BindableObject, BindableProperty, Color, Color)
SetBinding(BindableObject, BindableProperty, String, BindingMode, IValueConverter, String)

プロパティにバインドを作成し、適用します。

SetBinding<TSource>(BindableObject, BindableProperty, Expression<Func<TSource,Object>>, BindingMode, IValueConverter, String)
互換性のために残されています。

式からバインドを作成し適用します。

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

適用対象