2015-02-02 4 views
3

방금 ​​델파이로 라이브 바인딩을 발견했습니다. 그리고 주파수 변환기에 대한 제어 단어를 처리하기위한 첫 번째 구성 요소를 만들었습니다. 구성 요소 자체가 폼 디자이너에서 테스트를 잘 수행하는 것 같습니다. 그러나 응용 프로그램을 컴파일하고 실행하면 작동하지 않습니다. 이 같은 livbindings에서 스크린 샷 : 여기Delphi XE 라이브 바인딩 - 바이트 수

Livebindings screenshot

그리고 내가 livebinding으로이 작품을 가지고 부족하고 어떤 구성 요소 그래서

unit cBits2Byte; 

interface 

uses 
    System.SysUtils, System.Classes; 

type 
    TBits2Byte = class(TComponent) 
    private 
    { Private declarations } 
    fBit00, fBit01, fBit02, fBit03, fBit04, fBit05, fBit06, fBit07: Boolean; 
    function bitstate(sfr, bit: Byte): Boolean; 
    function ReadByte: Byte; 
    procedure WriteByte(aByte: Byte); 
    published 
    { Published declarations } 
    property char: byte read ReadByte write WriteByte; 

    property Bit00: Boolean read fBit00 write fBit00; 
    property Bit01: Boolean read fBit01 write fBit01; 
    property Bit02: Boolean read fBit02 write fBit02; 
    property Bit03: Boolean read fBit03 write fBit03; 
    property Bit04: Boolean read fBit04 write fBit04; 
    property Bit05: boolean read fBit05 write fBit05; 
    property Bit06: boolean read fBit06 write fBit06; 
    property Bit07: boolean read fBit07 write fBit07; 
    end; 

procedure Register; 

implementation 

procedure Register; 
begin 
    RegisterComponents('Standard', [TBits2Byte]); 
end; 

function TBits2Byte.bitstate(sfr, bit: Byte): Boolean; 
begin 
    Result := Boolean((sfr shr bit) And $01); 
end; 

function TBits2Byte.ReadByte: Byte; 
begin 
    Result := (Ord(Bit07) shl 7) Or 
      (Ord(Bit06) shl 6) Or 
      (Ord(Bit05) shl 5) Or 
      (Ord(Bit04) shl 4) Or 
      (Ord(Bit03) shl 3) Or 
      (Ord(Bit02) shl 2) Or 
      (Ord(Bit01) shl 1) Or 
      (Ord(Bit00)); 
end; 

procedure TBits2Byte.WriteByte(aByte: Byte); 
begin 
    Bit00 := bitstate(aByte, 0); 
    Bit01 := bitstate(aByte, 1); 
    Bit02 := bitstate(aByte, 2); 
    Bit03 := bitstate(aByte, 3); 
    Bit04 := bitstate(aByte, 4); 
    Bit05 := bitstate(aByte, 5); 
    Bit06 := bitstate(aByte, 6); 
    Bit07 := bitstate(aByte, 7); 
end; 

end. 

코드입니다?

답변

6

바인딩에 BindSource를 사용해야합니다. 이 예제에서는 단지 TPrototypeBindSource을 사용합니다.

데이터 개체에 대한 구성 요소가 없어도 간단한 개체이면 충분합니다. 바인딩

unit Form.Main; 

interface 

uses 
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, 
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.Bind.Components, 
    Data.Bind.ObjectScope, System.Rtti, System.Bindings.Outputs, Vcl.Bind.Editors, 
    Data.Bind.EngExt, Vcl.Bind.DBEngExt, Vcl.StdCtrls; 

type 
    TForm1 = class(TForm) 
    Bits2ByteSource: TPrototypeBindSource; 
     { OnCreateAdapter -> Bits2ByteSourceCreateAdapter } 
    CheckBox1: TCheckBox; 
    CheckBox2: TCheckBox; 
    CheckBox3: TCheckBox; 
    CheckBox4: TCheckBox; 
    CheckBox5: TCheckBox; 
    CheckBox6: TCheckBox; 
    CheckBox7: TCheckBox; 
    CheckBox8: TCheckBox; 
    Edit1: TEdit;  
    procedure Bits2ByteSourceCreateAdapter(Sender: TObject; var ABindSourceAdapter: TBindSourceAdapter); 
    private 
    { Private-Deklarationen } 
    public 
    { Public-Deklarationen } 
    end; 

var 
    Form1: TForm1; 

implementation 

uses 
    DataObject; 

{$R *.dfm} 

procedure TForm1.Bits2ByteSourceCreateAdapter(Sender: TObject; var ABindSourceAdapter: TBindSourceAdapter); 
begin 

    // create the adapter and an instance of the data object 

    ABindSourceAdapter := TObjectBindSourceAdapter<TBits2Byte>.Create(
    {AOwner} Self, 
    {AObject} TBits2Byte.Create, 
    {AOwnsObject} True); 

    ABindSourceAdapter.AutoEdit := True; 
    ABindSourceAdapter.AutoPost := True; 
end; 

end. 

와 지금

unit DataObject; 

interface 

type 
    TBits2Byte = class 
    private 
    { Private declarations } 
    fBit00, fBit01, fBit02, fBit03, fBit04, fBit05, fBit06, fBit07: Boolean; 
    function bitstate(sfr, bit: Byte): Boolean; 
    function ReadByte: Byte; 
    procedure WriteByte(aByte: Byte); 
    published 
    { Published declarations } 
    property char: Byte read ReadByte write WriteByte; 

    property Bit00: Boolean read fBit00 write fBit00; 
    property Bit01: Boolean read fBit01 write fBit01; 
    property Bit02: Boolean read fBit02 write fBit02; 
    property Bit03: Boolean read fBit03 write fBit03; 
    property Bit04: Boolean read fBit04 write fBit04; 
    property Bit05: Boolean read fBit05 write fBit05; 
    property Bit06: Boolean read fBit06 write fBit06; 
    property Bit07: Boolean read fBit07 write fBit07; 
    end; 

implementation 

function TBits2Byte.bitstate(sfr, bit: Byte): Boolean; 
begin 
    Result := Boolean((sfr shr bit) And $01); 
end; 

function TBits2Byte.ReadByte: Byte; 
begin 
    Result := 
    {} (Ord(Bit07) shl 7) Or 
    {} (Ord(Bit06) shl 6) Or 
    {} (Ord(Bit05) shl 5) Or 
    {} (Ord(Bit04) shl 4) Or 
    {} (Ord(Bit03) shl 3) Or 
    {} (Ord(Bit02) shl 2) Or 
    {} (Ord(Bit01) shl 1) Or 
    {} (Ord(Bit00) shl 0); 
end; 

procedure TBits2Byte.WriteByte(aByte: Byte); 
begin 
    Bit00 := bitstate(aByte, 0); 
    Bit01 := bitstate(aByte, 1); 
    Bit02 := bitstate(aByte, 2); 
    Bit03 := bitstate(aByte, 3); 
    Bit04 := bitstate(aByte, 4); 
    Bit05 := bitstate(aByte, 5); 
    Bit06 := bitstate(aByte, 6); 
    Bit07 := bitstate(aByte, 7); 
end; 

end. 

형태는 나머지는 livebindings으로 이루어집니다. 컨트롤 이륙을위한 Livebindings

준비에 모든 필드

 
Bit00 -> ftBoolean 
Bit01 -> ftBoolean 
Bit02 -> ftBoolean 
Bit03 -> ftBoolean 
Bit04 -> ftBoolean 
Bit05 -> ftBoolean 
Bit06 -> ftBoolean 
Bit07 -> ftBoolean 
Char -> ftChar 

Bits2Bytes Fields

그 바인드 후 : TPrototypeBindSource

선언 모든 필드는 I 필드 편집기로 Bits2ByteSource 이름 .


그냥 언급 :

TEdit.Text 값은 컨트롤을 떠난 후 업데이트됩니다. 해당 필드가 즉시 변경하려는 경우에는 (VCL) TEdit.OnChange/(FMX)와 TEdit.OnChangeTracking 이벤트를 설정해야

TLinkObservers.ControlChanged(Edit1); 

또는이

procedure TForm1.ControlChanged(Sender : TObject); 
begin 
    if Sender is TComponent then 
    TLinkObservers.ControlChanged(Sender as TComponent); 
end; 
에 대한 일반적인 방법이있을 것이다