表示: Delphi C++
表示設定

プロジェクト全体と得られた結果

提供:RAD Studio
移動: 案内, 検索

チュートリアル:LiveBinding をプログラムで使用する への移動


注意: ここで説明しているプログラム的手法は、バインディング式を実装する標準的な方法ではありません。VCL アプリケーションや FMX アプリケーションでは、通常なら、[オブジェクト インスペクタ](標準的な手法)を使用します。バインディング式の作成にプログラム的手法を用いる必要はないでしょう。ただし、そのような式を手動で作成することも可能であり、このチュートリアルではそれを例を用いて示します。
LiveBinding を標準的な方法で使用する例については、「チュートリアル:VCL アプリケーションで LiveBinding を使用する」および「チュートリアル:LiveBinding を使用してコードを書かずに FireMonkey アプリケーションを作成する」を参照してください。

このトピックでは、プロジェクトの全リストを示すほか、コンソール出力結果を解釈します。

プロジェクトの全ソース コード

 program LiveBindingProgr;
 
 {$APPTYPE CONSOLE}
 
 {$R *.res}
 
 uses
   System.SysUtils,
   System.Bindings.Expression,
   System.Bindings.ObjEval,
   System.Bindings.Helper;
 
 type
   TMyObject1 = class(TObject)
   private
     FIntegerValue: Integer;
     FStringValue: String;
   public
     property IntegerValue: Integer read FIntegerValue write FIntegerValue;
     property StringValue: String read FStringValue write FStringValue;
   end;
 
   TMyObject2 = class(TObject)
   private
     FIntegerValue: Integer;
     FStringValue: String;
   public
     property IntegerValue: Integer read FIntegerValue write FIntegerValue;
     property StringValue: String read FStringValue write FStringValue;
   end;
 
   TMyResultObject = class(TObject)
   private
     FIntegerValue: Integer;
     FStringValue: String;
   public
     property IntegerValue: Integer read FIntegerValue write FIntegerValue;
     property StringValue: String read FStringValue write FStringValue;
   end;
 
 var
   MyObject1: TMyObject1;
   MyObject2: TMyObject2;
   MyResultObject: TMyResultObject;
 
   BindingExpression1: TBindingExpression;
   BindingExpression2: TBindingExpression;
 
 begin
   MyObject1 := TMyObject1.Create;
   MyObject2 := TMyObject2.Create;
   MyResultObject := TMyResultObject.Create;
   try
     MyObject1.IntegerValue := 1;
     MyObject1.StringValue := 'LiveBinding ';
 
     MyObject2.IntegerValue := 2;
     MyObject2.StringValue := 'power.';
 
     { a binding expression that binds the two Integer properties of the given objects }
     BindingExpression1 := TBindings.CreateManagedBinding(
       { inputs }
       [TBindings.CreateAssociationScope([
         Associate(MyObject1, 'o1'),
         Associate(MyObject2, 'o2')
         ])],
       'o1.IntegerValue + o2.IntegerValue',
       { outputs }
       [TBindings.CreateAssociationScope([
         Associate(MyResultObject, 'res')
         ])],
       'res.IntegerValue',
       nil);
 
     { a binding expression that binds the two String properties of the given objects }
     BindingExpression2 := TBindings.CreateManagedBinding(
       { inputs }
       [TBindings.CreateAssociationScope([
         Associate(MyObject1, 'o1'),
         Associate(MyObject2, 'o2')
         ])],
       'o1.StringValue + o2.StringValue',
       { outputs }
       [TBindings.CreateAssociationScope([
         Associate(MyResultObject, 'res')
         ])],
       'res.StringValue',
       nil);
 
     TBindings.Notify(MyObject1, 'IntegerValue');
     TBindings.Notify(MyObject1, 'StringValue');
 
     Writeln('Result of add operation: ', MyResultObject.IntegerValue);
     Writeln(MyResultObject.StringValue);
     Readln;
   finally
     MyObject1.Free;
     MyObject2.Free;
     MyResultObject.Free;
   end;
 end.
ヒント: C++Builder コンソール アプリケーションを作成する場合は、プロジェクトが正しくコンパイルおよびリンクされるように、必ず[ビジュアル コンポーネント ライブラリ][ターゲット フレームワーク]として選択します。
 #pragma hdrstop
 #pragma argsused
 #pragma explicit_rtti methods()
 
 #include <tchar.h>
 #include <stdio.h>
 
 #include <System.Bindings.Expression.hpp>
 #include <System.Bindings.Helper.hpp>
 
 class TMyObject1 : public TObject {
 private:
 	Integer FIntegerValue;
 	String FStringValue;
 
 public:
 	__property Integer IntegerValue = {
 		read = FIntegerValue, write = FIntegerValue};
 	__property String StringValue = {read = FStringValue, write = FStringValue};
 };
 
 class TMyObject2 : public TObject {
 private:
 	Integer FIntegerValue;
 	String FStringValue;
 
 public:
 	__property Integer IntegerValue = {
 		read = FIntegerValue, write = FIntegerValue};
 	__property String StringValue = {read = FStringValue, write = FStringValue};
 };
 
 class TMyResultObject : public TObject {
 private:
 	Integer FIntegerValue;
 	String FStringValue;
 
 public:
 	__property Integer IntegerValue = {
 		read = FIntegerValue, write = FIntegerValue};
 	__property String StringValue = {read = FStringValue, write = FStringValue};
 };
 
 int _tmain(int argc, _TCHAR* argv[]) {
 	TMyObject1 *MyObject1;
 	TMyObject2 *MyObject2;
 	TMyResultObject *MyResultObject;
 
 	TBindingExpression *BindingExpression1, *BindingExpression2;
 
 	MyObject1 = new TMyObject1();
 	MyObject2 = new TMyObject2();
 	MyResultObject = new TMyResultObject();
 
 	MyObject1->IntegerValue = 1;
 	MyObject1->StringValue = "LiveBinding ";
 	MyObject2->IntegerValue = 2;
 	MyObject2->StringValue = "power.";
 
 	try {
 		try {
 			OpenArray<_di_IScope>InputScopes
 				(TBindings::CreateAssociationScope
 				(OPENARRAY(TBindingAssociation, (Associate(MyObject1, "o1"),
 				Associate(MyObject2, "o2")))));
 			OpenArray<_di_IScope>OutputScopes
 				(TBindings::CreateAssociationScope
 				(OPENARRAY(TBindingAssociation, (Associate(MyResultObject,
 				"res")))));
 
 			BindingExpression1 = TBindings::CreateManagedBinding(InputScopes,
 				InputScopes.GetHigh(), "o1.IntegerValue + o2.IntegerValue",
 				OutputScopes, OutputScopes.GetHigh(), "res.IntegerValue", NULL);
 
 			BindingExpression2 = TBindings::CreateManagedBinding(InputScopes,
 				InputScopes.GetHigh(), "o1.StringValue + o2.StringValue",
 				OutputScopes, OutputScopes.GetHigh(), "res.StringValue", NULL);
 
 			TBindings::Notify(MyObject1, "IntegerValue");
 			TBindings::Notify(MyObject1, "StringValue");
 
 			printf("Result of add operation: %d\n", MyResultObject->IntegerValue);
 			printf("%ls\n", MyResultObject->StringValue.c_str());
 		}
 		catch (Exception& e) {
 			printf("%ls\n", e.ToString().c_str());
 		}
 	}
 	__finally {
 		delete MyObject1;
 		delete MyObject2;
 		delete MyResultObject;
 	}
 
 	return 0;
 }

結果の解釈

このチュートリアルで説明したプログラムは、バインディング式を通じて 2 つの異なるオブジェクトの 4 つのプロパティをバインドするのに使用されています。これらのプロパティのうち、2 つは Integer 型で、これらを使って加算を実行しました。残りの 2 つのプロパティは string 型で、これらを使って文字列の連結を実行しました。

プログラムを実行すると、コンソール出力は以下のようになります。

 Result of add operation: 3
 LiveBinding power.

前のトピック

他言語版