Afficher : Delphi
C++
Préférences d'affichage
Le projet entier et l'observation des résultats
De RAD Studio
Remonter à Tutoriel : Utilisation de LiveBinding par programme
Attention : La méthode par programme décrite ici n'est PAS le moyen standard d'implémenter les expressions de liaison. Typiquement, vous devriez utiliser l'inspecteur d'objets (la méthode standard) dans une application VCL ou une application FMX. Vous ne devriez jamais avoir besoin d'utiliser la méthode de création d'expressions de liaison par programme. Toutefois, ce tutoriel montre qu'il est possible de créer manuellement de telles expressions.
Pour obtenir des exemples d'utilisation de LiveBindings selon la méthode standard, voir Tutoriel : Utilisation de LiveBinding dans les applications VCL et Tutoriel : Utilisation de LiveBinding pour créer une application FireMonkey sans code.
Cette rubrique contient le listing complet du projet et aussi l'interprétation des résultats de sortie de la console.
Le listing du projet
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.
Conseil : Lorsque vous créez une application console C++Builder, assurez-vous de choisir la bibliothèque des composants visuels comme Framework cible afin que les opérations de compilation et de liaison du projet s'effectuent correctement.
#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; }
Interprétation des résultats
Le programme décrit dans ce tutoriel est utilisé pour lier quatre propriétés de deux objets différents par le biais d'expressions de liaison. Deux de ces propriétés sont du type Integer ; vous avez effectué une addition en les utilisant. Les deux autres propriétés sont du type string ; vous avez effectué une concaténation en les utilisant.
En exécutant le programme, la sortie de la console doit être comme suit.
Result of add operation: 3 LiveBinding power.