7

我正在尝试使用自定义类的数组作为我的组件的属性,但问题是值没有保存到组件中,这意味着如果我设置值,保存所有内容并再次打开项目,组件的值消失了...我的代码如下所示:

unit Unit1;

interface

uses  Windows, ExtCtrls,Classes,Controls;

type

  TMyClass=class(TPersistent)
  private
    FName: string;
    FValue: double;
  public
    property Name: string read FName write FName;
    property Value: double read FValue write FValue;
  end;

  TMyComponent= class(TCustomPanel)
  private
    FMyArray: array[0..200] of TMyClass;

    function GetmyArray(Index: Integer): TMyClass;

    procedure SetMyArray(index: Integer; Value: TMyClass);
  public
    property myArray[index: Integer]: TMyClass read GetMyArray write SetMyArray;
  end;

implementation

function TMyComponent.GetmyArray(Index: Integer): TMyClass;
begin
  result:= FmyArray[Index];
end;

procedure TMyComponent.SetMyArray(index: Integer; Value: TMyClass);
begin
  FMyArray[index].FName:= Value.FName;
  FMyArray[index].FValue:= Value.FValue;
end;

end.

我知道只能流式传输已发布的属性,但问题是我的属性是一个数组并且无法发布...我的建议是DefineProperties()用来提供自定义流式传输,但我不知道如何用数组来做到这一点。我认为的另一种可能性是将 TMyClass 修改为一种 TMyComponent 可能是它的父类的类,就像在 TChart 中所做的那样,您可以向其中添加不同的系列类。但我不知道这应该是什么类

TMyClass=class(T???????????)

有了它,我可以取出属性 MyArray 并创建 TMyClass 并添加到 TMyComponent ,如下所示:

MyArray1.parent:= MyComponent1;
MyArray2.parent:= MyComponent2;
...

. 哪一个是更好的选择?或者还有其他更好的主意吗?

4

2 回答 2

14

最简单(也是首选)的解决方案是更改TMyClass为派生自TCollectionItem和更改TMyComponent.FMyArrayTOwnedCollection. TMyClass然后 DFM 将自动为您流式传输项目,并且您获得用于创建和操作对象及其属性的本机设计时支持。

尝试这个:

unit Unit1;

interface

uses
  Windows, ExtCtrls, Classes, Controls;

type
  TMyClass = class(TCollectionItem)
  private
    FName: string;
    FValue: double;

    procedure SetName(const AValue: string);
    procedure SetValue(AValue: double);
  public
    procedure Assign(ASource: TPersistent); override;
  published
    property Name: string read FName write SetName;
    property Value: double read FValue write SetValue;
  end;

  TMyArray = class(TOwnedCollection)
  private
    function  GetItem(Index: Integer): TMyClass;
    procedure SetItem(Index: Integer; const Value: TMyClass);
  public
    constructor Create(AOwner: TPersistent);
    function  Add: TMyClass; reintroduce;
    function  Insert(Index: Integer): TMyClass; reintroduce;
    property  Items[Index: Integer]: TMyClass read GetItem write SetItem; default;
  end;

  TMyComponent = class(TCustomPanel)
  private
    FMyArray: TMyArray;

    procedure SetMyArray(Value: TMyArray);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property myArray: TMyArray read FMyArray write SetMyArray;
  end;

implementation

procedure TMyClass.Assign(ASource: TPersistent);
begin
  if ASource is TMyClass then
  begin
    with TMyClass(ASource) do
    begin
      Self.FName := Name;
      Self.FValue := Value;
    end;
    Changed(False);
  end else
    inherited;
end;

procedure TMyClass.SetName(const AValue: string);
begin
  if FName <> AValue then
  begin
    FName := AValue;
    Changed(False);
  end;
end;

procedure TMyClass.SetValue(AValue: double);
begin
  if FValue <> AValue then
  begin
    FValue := AValue;
    Changed(False);
  end;
end;

constructor TMyArray.Create(AOwner: TPersistent);
begin
  inherited Create(AOwner, TMyClass);
end;

function TMyArray.GetItem(Index: Integer): TMyClass;
begin
  Result := TMyClass(inherited GetItem(Index));
end;

procedure TMyArray.SetItem(Index: Integer; const Value: TMyClass);
begin
  inherited SetItem(Index, Value);
end;

function TMyArray.Add: TMyClass;
begin
  Result := TMyClass(inherited Add);
end;

function TMyArray.Insert(Index: Integer): TMyClass;
begin
  Result := TMyClass(inherited Insert(Index));
end;

constructor TMyComponent.Create(AOwner: TComponent);
begin
  inherited;
  FMyArray := TMyArray.Create(Self);
end;

destructor TMyComponent.Destroy;
begin
  FMyArray.Free;
  inherited;
end;

procedure TMyComponent.SetMyArray(Value: TMyArray);
begin
  FMyArray.Assign(Value);
end;

end.
于 2013-05-29T18:48:55.797 回答
5

我会投票给DefineProperties!必要的代码可能如下所示(假设数组中的所有实例都不是 nil):

procedure TMyComponent.DefineProperties(Filer: TFiler);
begin
  inherited;
  Filer.DefineProperty('MyArray', ReadMyArray, WriteMyArray, true);
end;

procedure TMyComponent.ReadMyArray(Reader: TReader);
var
  N: Integer;
begin
  N := 0;
  Reader.ReadListBegin;
  while not Reader.EndOfList do begin
    Reader.ReadListBegin;
    FMyArray[N].Name := Reader.ReadString;
    FMyArray[N].Value := Reader.ReadFloat;
    Reader.ReadListEnd;
    Inc(N);
  end;
  Reader.ReadListEnd;
end;

procedure TMyComponent.WriteMyArray(Writer: TWriter);
var
  I: Integer;
begin
  Writer.WriteListBegin;
  for I := 0 to High(FMyArray) do begin
    Writer.WriteListBegin;
    Writer.WriteString(FMyArray[I].Name);
    Writer.WriteFloat(FMyArray[I].Value);
    Writer.WriteListEnd;
  end;
  Writer.WriteListEnd;
end;
于 2013-05-29T16:46:17.237 回答