Delphi XE LiveBindings - 位到字节
Delphi XE LiveBindings - Bits to Byte
我刚刚发现了 Delphi 的实时绑定。并创建了我的第一个组件来处理变频器的控制字。
它本身的组件似乎可以很好地在表单设计器中对其进行测试。但是,编译和 运行 应用程序不起作用。
来自 livbindings 的屏幕截图如下:
这里是组件的代码
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 );
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.
那么,我缺少什么才能将此作品作为实时绑定?
您必须使用 BindSource 进行绑定。在此示例中,我仅使用 TPrototypeBindSource
.
数据对象不需要组件,一个简单的对象就够了。
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 );
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.
现在带有绑定的表单
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.
剩下的就是使用实时绑定完成了。
声明 TPrototypeBindSource
我用字段编辑器命名为 Bits2ByteSource
的所有字段:
Bit00 -> ftBoolean
Bit01 -> ftBoolean
Bit02 -> ftBoolean
Bit03 -> ftBoolean
Bit04 -> ftBoolean
Bit05 -> ftBoolean
Bit06 -> ftBoolean
Bit07 -> ftBoolean
Char -> ftChar
然后将所有字段与控件绑定
准备起飞。
顺便提一下:
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;
我刚刚发现了 Delphi 的实时绑定。并创建了我的第一个组件来处理变频器的控制字。 它本身的组件似乎可以很好地在表单设计器中对其进行测试。但是,编译和 运行 应用程序不起作用。 来自 livbindings 的屏幕截图如下:
这里是组件的代码
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 );
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.
那么,我缺少什么才能将此作品作为实时绑定?
您必须使用 BindSource 进行绑定。在此示例中,我仅使用 TPrototypeBindSource
.
数据对象不需要组件,一个简单的对象就够了。
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 );
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.
现在带有绑定的表单
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.
剩下的就是使用实时绑定完成了。
声明 TPrototypeBindSource
我用字段编辑器命名为 Bits2ByteSource
的所有字段:
Bit00 -> ftBoolean Bit01 -> ftBoolean Bit02 -> ftBoolean Bit03 -> ftBoolean Bit04 -> ftBoolean Bit05 -> ftBoolean Bit06 -> ftBoolean Bit07 -> ftBoolean Char -> ftChar
然后将所有字段与控件绑定
准备起飞。
顺便提一下:
TEdit.Text
值仅在离开控件后更新。如果您想立即更改该字段,则必须使用
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;