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

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

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


注意: ここで説明しているプログラム的手法は、バインディング式を実装する標準的な方法ではありません。グラフィカル アプリケーションでは、通常は、[オブジェクト インスペクタ](標準的な手法)を使用します。バインディング式をプログラムで作成する必要はないでしょう。ただし、そのような式を手動で作成することも可能であり、このチュートリアルではそれを例を用いて示します。


標準的な方法での LiveBinding の使用例については、以下を参照してください。

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

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

Delphi の場合:

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.';

    { 与えられたオブジェクトの 2 つの Integer 型プロパティをバインドするバインディング式 }
    BindingExpression1 := TBindings.CreateManagedBinding(
      { 入力 }
      [TBindings.CreateAssociationScope([
        Associate(MyObject1, 'o1'),
        Associate(MyObject2, 'o2')
        ])],
      'o1.IntegerValue + o2.IntegerValue',
      { 出力 }
      [TBindings.CreateAssociationScope([
        Associate(MyResultObject, 'res')
        ])],
      'res.IntegerValue',
      nil);

    { 与えられたオブジェクトの 2 つの String 型プロパティをバインドするバインディング式 }
    BindingExpression2 := TBindings.CreateManagedBinding(
      { 入力 }
      [TBindings.CreateAssociationScope([
        Associate(MyObject1, 'o1'),
        Associate(MyObject2, 'o2')
        ])],
      'o1.StringValue + o2.StringValue',
      { 出力 }
      [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 コンソール アプリケーションを作成する場合は、プロジェクトが正しくコンパイルおよびリンクされるように、必ず[ビジュアル コンポーネント ライブラリ][ターゲット フレームワーク]として選択します。

C++ の場合:

#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.

前のトピック