我有一个具有已发布属性的组件。想象一下:
TSomething = (sOne, sTwo, sThree, ...);
TSomethings = set of TSomething;
TSomeComponent = TWinControl
private
fSomethings: TSomethings;
function GetSomethings: string;
procedure SetSomethings(const Value: TSomethings);
published
property Somethings: TSomethings read GetSomethings write SetSomethings;
end;
问题是 TSomething 现在已达到已发布集的最大容量(编译器错误 E2187),因此我需要按如下方式拆分该集:
TSomething = (sTwo, sThree, ...);
TSomethings = set of TSomething;
// Some items moved from TSomething and named with an "o" prefix
TOtherThing = (oOne, oThirtyThree, oThirtyFour, ...);
TOtherThings = set of TOtherThing;
TSomeComponent = TWinControl
private
fSomethings: TSomethings;
fOtherThings: TOtherThings;
function GetSomethings: string;
procedure SetSomethings(const Value: TSomethings);
function GetOtherThings: string;
procedure SetOtherThings(const Value: TOtherThings);
published
property Somethings: TSomethings read GetSomethings write SetSomethings;
property OtherThings: TOtherThings read GetOtherThings write SetOtherThings;
end;
TSomethings 中的一些现有项目已移至 TOtherThings(按逻辑对它们进行分类),因此一些用户会打开他们的表单以显示错误:
Error reading SomeComponent1.Somethings: Invalid property value
所以我想在打开表单时默默地处理更改。换句话说,拦截 DFM 中的 Somethings 属性的读取并填充 Somethings 和 OtherThings 属性。
但我似乎找不到办法做到这一点。如果我愿意完全重命名旧属性,覆盖 DefineProperties 应该可以工作,但这会不必要地破坏很多代码(治愈比疾病更糟糕)。
那么还有另一种我忽略的方法吗?
类似的问题:
最佳答案
您不需要重命名现有属性,只需将它们设为 public
而不是 published
在不破坏任何用户代码的情况下为他们禁用 native DFM 流,然后覆盖 DefineProperties()
根据需要处理属性的 DFM 流。
如果您需要维护设计时支持,您可以尝试 creating "fake" properties用于显示对象检查器,以便用户可以根据需要编辑公共(public)属性的值。或者,只编写一个自定义组件编辑器而不是显示一个模态表单来使用 TCheckListBox
编辑值可能会更容易。 , 或 TListView
带有复选框。
关于delphi - 可以拦截对现有已发布属性的读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56123648/