BindableObject クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
アプリケーション開発者が検証、強制型変換、イベント システムを有効にすることで、あるオブジェクトのデータに対する変更を別のオブジェクトに適用できるメカニズムを提供します。 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 ます。
次のコードは、上記のクラスを参照するプロジェクトに含まれている場合、 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 | |
メソッド
イベント
| BindingContextChanged |
BindingContext プロパティが変更されるたびに発生します。 |
| PropertyChanged |
プロパティが変更されたときに発生します。 |
| PropertyChanging |
プロパティが変更されようとしているときに発生します。 |
明示的なインターフェイスの実装
| IDynamicResourceHandler.SetDynamicResource(BindableProperty, String) |
Xamarin.Forms プラットフォームによる内部使用向け。 |