B
bannico
Unregistered / Unconfirmed
GUEST, unregistred user!
使用第三方控件:NativeXml。
最终目的:保存Thumbs这个对象时,能够将Thumbs所拥有的Thumb也保存起来。以下是一个简单的演示代码:
unit Thumbs;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,NativeXml,NativeXmlObjectStorage, Contnrs;
type
TThumb = class (TPersistent)
private
FBitmap:TBitmap;
FCaption:string;
public
constructor Create;
destructor Destroy; override;
published
property Bitmap:TBitmap read FBitmap write FBitmap;
property Caption:string read FCaption write FCaption;
end;
TThumbs = class (TPersistent)
private
FList:TObjectList;
function getThumb(Index:Integer):TThumb;
function getCount:Integer;
public
constructor Create;
destructor Destroy; override;
procedure SaveToFile(aFileName:string);
property Thumb[Index:Integer]:TThumb read getThumb;
published
property Count:Integer read GetCount;
//这里应该设置什么属性才能将FList里面的Thumb持久保存下来??
//也就是:我该如何写SaveToFile才能持久化保存我想要保存的内容?
end;
implementation
{ TThumb }
constructor TThumb.Create;
begin
FBitmap:=TBitmap.Create;
FBitmap.Width:=20;
FBitmap.Height:=20;
end;
destructor TThumb.Destroy;
begin
FBitmap.Free;
inherited;
end;
{ TThumbs }
constructor TThumbs.Create;
var
I:Integer;
Thumb:TThumb;
begin
//因演示关系,所以在这里添加子类。
FList:=TObjectList.Create;
for I:=0 to 19 do
begin
Thumb:=TThumb.Create;
Thumb.Caption:=IntToStr(I);
FList.Add(Thumb);
end;
end;
destructor TThumbs.Destroy;
begin
FList.Clear;
FList.Free;
inherited;
end;
function TThumbs.getCount: Integer;
begin
Result:=FList.Count;
end;
function TThumbs.getThumb(Index: Integer): TThumb;
begin
Result:=TThumb(FList.Items[Index]);
end;
procedure TThumbs.SaveToFile(aFileName: string);
var
ADoc: TNativeXml;
AWriter: TsdXmlObjectWriter;
begin
ADoc := TNativeXml.CreateName('Root');
try
ADoc.Utf8Encoded := True;
ADoc.EncodingString := 'UTF-8';
AWriter := TsdXmlObjectWriter.Create;
try
//不想在循环里面这样:
//for I:=0 to Count - 1 do
//AWriter.WriteObject(ADoc.Root,Self.Thumb[0], Nil);
AWriter.WriteObject(ADoc.Root,Self, Nil);
finally
AWriter.Free;
end;
ADoc.XmlFormat := xfReadable;
finally
ADoc.SaveToFile(aFileName);
ADoc.Free;
end;
end;
end.
最终目的:保存Thumbs这个对象时,能够将Thumbs所拥有的Thumb也保存起来。以下是一个简单的演示代码:
unit Thumbs;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,NativeXml,NativeXmlObjectStorage, Contnrs;
type
TThumb = class (TPersistent)
private
FBitmap:TBitmap;
FCaption:string;
public
constructor Create;
destructor Destroy; override;
published
property Bitmap:TBitmap read FBitmap write FBitmap;
property Caption:string read FCaption write FCaption;
end;
TThumbs = class (TPersistent)
private
FList:TObjectList;
function getThumb(Index:Integer):TThumb;
function getCount:Integer;
public
constructor Create;
destructor Destroy; override;
procedure SaveToFile(aFileName:string);
property Thumb[Index:Integer]:TThumb read getThumb;
published
property Count:Integer read GetCount;
//这里应该设置什么属性才能将FList里面的Thumb持久保存下来??
//也就是:我该如何写SaveToFile才能持久化保存我想要保存的内容?
end;
implementation
{ TThumb }
constructor TThumb.Create;
begin
FBitmap:=TBitmap.Create;
FBitmap.Width:=20;
FBitmap.Height:=20;
end;
destructor TThumb.Destroy;
begin
FBitmap.Free;
inherited;
end;
{ TThumbs }
constructor TThumbs.Create;
var
I:Integer;
Thumb:TThumb;
begin
//因演示关系,所以在这里添加子类。
FList:=TObjectList.Create;
for I:=0 to 19 do
begin
Thumb:=TThumb.Create;
Thumb.Caption:=IntToStr(I);
FList.Add(Thumb);
end;
end;
destructor TThumbs.Destroy;
begin
FList.Clear;
FList.Free;
inherited;
end;
function TThumbs.getCount: Integer;
begin
Result:=FList.Count;
end;
function TThumbs.getThumb(Index: Integer): TThumb;
begin
Result:=TThumb(FList.Items[Index]);
end;
procedure TThumbs.SaveToFile(aFileName: string);
var
ADoc: TNativeXml;
AWriter: TsdXmlObjectWriter;
begin
ADoc := TNativeXml.CreateName('Root');
try
ADoc.Utf8Encoded := True;
ADoc.EncodingString := 'UTF-8';
AWriter := TsdXmlObjectWriter.Create;
try
//不想在循环里面这样:
//for I:=0 to Count - 1 do
//AWriter.WriteObject(ADoc.Root,Self.Thumb[0], Nil);
AWriter.WriteObject(ADoc.Root,Self, Nil);
finally
AWriter.Free;
end;
ADoc.XmlFormat := xfReadable;
finally
ADoc.SaveToFile(aFileName);
ADoc.Free;
end;
end;
end.