Tôi đang cập nhật một số thuộc tính trong một thành phần. Để tránh các lỗi thuộc tính bị thiếu Tôi đang sử dụng DefineProperties để đọc các thuộc tính cũ từ luồng. Hầu hết các thuộc tính hoạt động tốt, ví dụ: Số nguyên, nhưng tôi không thể nhận được các thuộc tính dựa trên TPersistent để hoạt động. Thủ tục ReadProperty (TPersistent) trong TReader được bảo vệ, không công khai và yêu cầu phải truy cập vào nó. Thậm chí sau đó, thủ tục ReadFontProperty không bao giờ được gọi và ngoại lệ thuộc tính bị thiếu xảy ra.Sử dụng DefineProperties để thay thế các thuộc tính TPersistent ví dụ: TFont
Làm cách nào để đọc tài sản TFont?
Dưới đây là một số mã mẫu về cách tôi đang cố gắng thực hiện.
...
type
TMyComponent = class(TComponent)
strict private
// Removed
//FIntegerProperty: Integer;
//FFontProperty: TFont;
// New
FNewIntegerProperty: Integer;
FNewFontProperty: TFont;
procedure ReadIntegerProperty(Reader: TReader);
procedure ReadFontProperty(Reader: TReader);
protected
procedure DefineProperties(Filer: TFiler); override;
published
// Removed properties
//property IntegerProperty: Integer read FIntegerProperty write FIntegerProperty;
//property FontProperty: TFont read FFontProperty write SetFontProperty;
// New properties
property NewIntegerProperty: Integer read FNewIntegerProperty write FNewIntegerProperty;
property NewFontProperty: TFont read FNewFontProperty write SetNewFontProperty;
end;
implementation
procedure TMyComponent.DefineProperties(Filer: TFiler);
begin
inherited;
// This works
Filer.DefineProperty('IntegerProperty', ReadIntegerProperty, nil, FALSE);
// This doesn't
Filer.DefineProperty('FontProperty', ReadFontProperty, nil, FALSE);
end;
procedure TMyComponent.ReadIntegerProperty(Reader: TReader);
begin
FNewIntegerProperty:= Reader.ReadInteger;
end;
type
THackReader = class(TReader);
procedure TMyComponent.ReadFontProperty(Reader: TReader);
begin
{ TODO : This doesn't work. How do we read fonts? }
THackReader(Reader).ReadProperty(FNewFontProperty);
end;
...
Cập nhật 1
Cố gắng gợi ý của David sử dụng đoạn mã sau:
Filer.DefineProperty('Font.CharSet', ReadFontCharSet, nil, False);
...
procedure TMyComponent.ReadFontCharSet(Reader: TReader);
begin
Reader.ReadInteger;
end;
tôi nhận được một Invalid Property Value lỗi. Tôi đoán đó là điều cần làm với Charset là loại TFontCharset (= System.UITypes.TFontCharset = 0..255). Làm cách nào để đọc loại tài sản này?
@norgepaul Bạn đã tham gia bằng cách nào? Tôi khá chắc chắn đó là câu trả lời cho câu hỏi của bạn. –
Xin chào David. Cảm ơn câu trả lời. Tôi chắc chắn nó là chính xác, nhưng tôi đã không có cơ hội để kiểm tra nó được nêu ra. Tôi sẽ cố gắng để có được nó sớm. – norgepaul
OK, tôi chỉ tự hỏi liệu bạn có gặp vấn đề và tò mò không. –