Delphi: Making a component visible to live binding

2019-04-11 01:05发布

问题:

I have been trying to make a test object that has a string property visible to the visual binding form. The component is registered with the appropriate properties. Using XE8 and Firemonkey.

I can get it to show on the visual binder by selecting it using the hide show elements, however I cannot get it to bind the Foo string to a TEdit text property, the readonly (which does nothing yet) will bind to the Text Property.

  • how do I get it show in it with default class visibility
  • why doesn't the Foo string bind to the text property

The help files are a little thin on this. I guessing I'm missing something really simple here.

Object Code is below.

Thanks.

unit TestOBj;

interface

uses
  System.Classes, System.SysUtils, System.StrUtils, Data.Bind.Components;

Type

  TMyLBObject = Class (TComponent)
  private
    fFooString:String;
    FReadOnly: Boolean;
    procedure SetReadOnly(const Value: Boolean);
    procedure SetFooString(const Value: String);
  protected
    function CanObserve(const ID: Integer): Boolean; override;                       
    procedure ObserverAdded(const ID: Integer; const Observer: IObserver); override;
    procedure ObserverToggle(const AObserver: IObserver; const Value: Boolean);
  published
    property Foo:String read FFooString write SetFooString;
    property ReadOnly:Boolean read FReadOnly write SetReadOnly;
  End;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Test', [TMyLBObject]);
end;

{ TMyLBObject }

function TMyLBObject.CanObserve(const ID: Integer): Boolean;
begin
  Result := False;
  if ID = TObserverMapping.EditLinkID then
    Result := True;
  if ID = TObserverMapping.ControlValueID then
    Result := True;
end;

procedure TMyLBObject.ObserverAdded(const ID: Integer;
  const Observer: IObserver);
begin
  if ID = TObserverMapping.EditLinkID then
    Observer.OnObserverToggle := ObserverToggle;
end;


procedure TMyLBObject.ObserverToggle(const AObserver: IObserver;
  const Value: Boolean);
var
  LEditLinkObserver: IEditLinkObserver;
  LSaveReadOnly: Boolean;
begin
  if Supports(AObserver, IEditLinkObserver, LEditLinkObserver) then
  begin
    if Value then
    begin
      LSaveReadOnly := ReadOnly;
      if LEditLinkObserver.IsReadOnly then
        ReadOnly := True;
      FReadOnly := LSaveReadOnly;
    end
    else
      if not (csDestroying in ComponentState) then
        ReadOnly := FReadOnly;
  end;
end;


procedure TMyLBObject.SetFooString(const Value: String);
begin
  FFooString := Value;
end;

procedure TMyLBObject.SetReadOnly(const Value: Boolean);
begin
  FReadOnly := Value;
end;


initialization
  RegisterClass(TMyLBObject);
  RegisterObservableMember(TArray<TClass>.Create(TMyLBObject), 'Foo', 'FMX');
finalization
  UnRegisterObservableMember(TArray<TClass>.Create(TMyLBObject));
end.

回答1:

Your TMyLBObject component does not have an [ObservableMember('Foo')] attribute.

type
  [ObservableMember('Foo')] // <-- add this
  TMyLBObject = Class (TComponent)
    ...
  published
    property Foo:String read FFooString write SetFooString;
    ...
  End;

See the example in the documentation:

Tutorial: Creating LiveBindings-Enabled Components.

Which includes this note:

The ObservableMember attribute will be used by LiveBindings components to generate expressions. RegisterObserverableMember() will be used at design time to inform the LiveBindings Designer.