我試圖爲某些自定義組件創建一個自定義屬性編輯器。自定義屬性編輯器旨在編輯一些設置屬性,如創建自定義TSetProperty屬性編輯器
type
TButtonOption = (boOption1, boOption2, boOption3);
TButtonOptions = set of TButtonOption;
我的屬性編輯器從TSetProperty類下降。問題是:我的自定義屬性編輯器沒有得到註冊,Delphi IDE似乎使用它自己的默認設置屬性編輯器,因爲屬性編輯器方法中的ShowMessage()調用從不執行!我從頭創建了一個示例包/組件,儘可能簡單,顯示了這個問題。下面是代碼:
unit Button1;
interface
uses
System.SysUtils, System.Classes, Vcl.Controls, Vcl.StdCtrls, DesignIntf, DesignEditors;
type
TButtonOption = (boOption1, boOption2, boOption3);
TButtonOptions = set of TButtonOption;
TButtonEx = class(TButton)
private
FOptions: TButtonOptions;
function GetOptions: TButtonOptions;
procedure SetOptions(Value: TButtonOptions);
published
property Options: TButtonOptions read GetOptions write SetOptions default [];
end;
TMySetProperty = class(TSetProperty)
public
function GetAttributes: TPropertyAttributes; override;
procedure GetProperties(Proc: TGetPropProc); override;
function GetValue: string; override;
end;
procedure Register;
implementation
uses
Dialogs;
// TButtonEx - sample component
function TButtonEx.GetOptions: TButtonOptions;
begin
Result := FOptions;
end;
procedure TButtonEx.SetOptions(Value: TButtonOptions);
begin
if FOptions <> Value then
begin
FOptions := Value;
end;
end;
// register stuff
procedure Register;
begin
RegisterComponents('Samples', [TButtonEx]);
RegisterPropertyEditor(TypeInfo(TButtonOptions), nil, '', TMySetProperty);
end;
function TMySetProperty.GetAttributes: TPropertyAttributes;
begin
ShowMessage('GetAttributes');
Result := inherited GetAttributes;
end;
procedure TMySetProperty.GetProperties(Proc: TGetPropProc);
begin
ShowMessage('GetProperties');
inherited;
end;
function TMySetProperty.GetValue: string;
begin
ShowMessage('GetValue');
Result := inherited GetValue;
end;
end.
請注意:
- 我註冊用於具有TButtonOptions屬性中的所有組件的新屬性編輯器(TMySetProperty)。我也試過只爲TButtonEx做,但結果是一樣的。
- 我在我的自定義屬性編輯器的所有重寫方法中添加了ShowMessage()調用,並且這些方法永遠不會被調用。
- 我已經調試了包並且執行了RegisterPropertyEditor()。不過,我重寫的方法中的自定義代碼從不執行。
- 我已經看到其他第三方組件使用這種屬性編輯器(TSetProperty後代)在舊的Delphi IDE中運行,我無法找到任何相關的代碼差異。也許Delphi XE2 +需要別的東西?
所以問題是: 爲什麼我的自定義屬性編輯器沒有註冊/工作?
注意:至少在Delphi XE2,XE3,XE4和XE5中會發生此問題。其他IDE未經測試,但可能具有相同的行爲。
您不能實現你在同一個封裝組件和編輯器。該組件需要位於其自己的僅運行時軟件包中,並且該編輯器需要位於僅需要運行時軟件包的僅包含designtime的軟件包中。 –
嗨,雷米。感謝您的答覆。不幸的是,這不是問題所在。這裏的代碼被簡化了(爲了清楚起見),所有的代碼都在同一個單元內。我真正的代碼使用2個不同的包,一個用於運行時的東西,另一個用於設計時間的東西問題是一樣的。自定義屬性編輯器不能以任何方式工作。 – Alexandre
我可以在XE2中重現問題,我不知道爲什麼它不起作用。很顯然,設計時包依靠「RegisterComponents()」工作。所以我添加了另一個基於'Integer'的屬性併爲它註冊了一個編輯器,並且它工作正常。所以這個問題必須是IDE處理基於Set的屬性的一個問題。 –