Delphi: 使组件对实时绑定可见

Delphi: Making a component visible to live binding

我一直在尝试使具有字符串 属性 的测试对象对可视绑定表单可见。该组件注册了适当的属性。使用 XE8 和 Firemonkey。

我可以通过使用隐藏显示元素选择它来让它显示在可视活页夹上,但是我无法让它将 Foo 字符串绑定到 TEdit 文本 属性,只读(什么都不做yet) 将绑定到文本 属性.

帮助文件在这方面有点单薄。我想我在这里遗漏了一些非常简单的东西。

目标代码如下。

谢谢。

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.

您的 TMyLBObject 组件没有 [ObservableMember('Foo')] 属性。

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

参见文档中的示例:

Tutorial: Creating LiveBindings-Enabled Components.

其中包括这条注释:

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