Delphi manages to have TLabel
exist in FMX and VCL. So, how do I create two components, both with the same name, except one is for VCL and one is for FMX?
Yes, I know I can use ifdef
s and recompile the library every time. But that is not exactly clean code.
CodePudding user response:
Implement your two components in different Unit Scopes, which were created for this exact purpose.
For example, implement TMyComponent
for VCL in Vcl.MyUnit.pas
, and implement TMyComponent
for FMX in FMX.MyUnit.pas
.
Then, to use TMyComponent
in any other unit, you can either:
use
{$IFDEF}
s to conditionally specifyVcl.MyUnit
orFMX.MyUnit
in theuses
clause.create separate projects for VCL and FMX, where the VCL project specifies
Vcl
in its Unit Scope Names compiler setting, and the FMX project specifiesFMX
, and then you can use justMyUnit
in theuses
clause.
Also see Adding Unit Scope Names for Your Own Components.
For reference, VCL's TLabel
is in the Vcl.StdCtrls
unit scope, and FMX's TLabel
is in the FMX.StdCtrls
unit scope.