【Delphi】代码化 LiveBinding

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.

关键词:Tutorial: Using LiveBinding Programatically

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海纳老吴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值