LoadChartFromStream
在SaveChartToStream
保存了我的一些自定义属性时引发异常。
它与";属性MyCoolProperty不存在";。
type
TMyChart = class(TChart)
strict private
MyCoolProperty: Integer;
published
property MyCoolProperty: Integer read FMyCoolProperty write FMyCoolProperty;
end;
Chart, CopyChart: TMyChart;
MStream := TMemoryStream.Create;
try
SaveChartToStream(Chart, MStream, False, False);
MStream.Position := 0;
LoadChartFromStream(TCustomChart(CopyChart), MStream);
finally
MStream.Free;
end;
如何让LoadChartFromStream
忽略它无法处理的属性,加载它可以加载的属性而不会因为一个小部分而破坏整体?
让我从这里复制回复,解决了类似的问题
您是否尝试过重写
DefineProperties
并在其中添加要序列化的属性?这对我来说很好:uses TeeStore; type TMyBarSeries = class(TBarSeries) private procedure WriteBarName(Writer: TWriter); procedure ReadBarName(Reader: TReader); procedure WriteBarID(Writer: TWriter); procedure ReadBarID(Reader: TReader); protected IBarNameStored: boolean; IBarName: string; IBarID: Integer; procedure DefineProperties(Filer:TFiler); override; public property BarName: string read IBarName write IBarName stored IBarNameStored; property BarID: Integer read IBarID write IBarID default -1; end; procedure TMyBarSeries.DefineProperties(Filer: TFiler); begin inherited; Filer.DefineProperty('BarName',ReadBarName,WriteBarName,True); Filer.DefineProperty('BarID',ReadBarID,WriteBarID,True); end; procedure TMyBarSeries.WriteBarName(Writer: TWriter); begin Writer.WriteIdent(BarName); end; procedure TMyBarSeries.ReadBarName(Reader: TReader); begin BarName:=Reader.ReadIdent; end; procedure TMyBarSeries.WriteBarID(Writer: TWriter); begin Writer.WriteInteger(BarID); end; procedure TMyBarSeries.ReadBarID(Reader: TReader); begin BarID:=Reader.ReadInteger; end; procedure TForm1.FormCreate(Sender: TObject); var memStream1: TStream; begin RegisterClass(TMyBarSeries); Chart1.View3D:=false; Chart1.Legend.Visible:=false; with Chart1.AddSeries(TMyBarSeries) as TMyBarSeries do begin BarID:=1; BarName:='MyBar #' + IntToStr(BarID); FillSampleValues; ColorEachPoint:=true; end; memStream1:=TMemoryStream.Create; SaveChartToStream(Chart1, memStream1); memStream1.Position:=0; LoadChartFromStream(Chart2, memStream1); Chart2.Top:=Chart1.Top+Chart1.Height+10; Memo1.Clear; Memo1.Lines.Add(IntToStr((Chart1[0] as TMyBarSeries).BarID)); Memo1.Lines.Add((Chart1[0] as TMyBarSeries).BarName); Memo1.Lines.Add(IntToStr((Chart2[0] as TMyBarSeries).BarID)); Memo1.Lines.Add((Chart2[0] as TMyBarSeries).BarName); end;