Jer*_*dge 13 delphi vcl firemonkey
我有一个TComponent控制一些UI.此组件旨在通过使用条件来支持VCL和Firemonkey.此类条件指示我的组件是接受VCL控件还是FMX控件.目前预计此条件在应用程序级别定义,以指示组件在运行时是否管理VCL或FMX控件.
我想将我的组件发布到IDE中,同时支持VCL和FMX,与条件共享相同的单元.但是,根据当前是否正在使用VCL或FMX,属性名称/类型会有所不同.
例如...
type
TMyComponent = class(TComponent)
published
{$IFDEF USE_FMX}
property TabControl: TTabControl read FTabControl write SetTabControl;
{$ENDIF}
{$IFDEF USE_VCL}
property PageControl: TPageControl read FPageControl write SetPageControl;
{$ENDIF}
end;
Run Code Online (Sandbox Code Playgroud)
我的目标是能够将这个非可视组件放到VCL或FMX表单上,并在对象检查器中自动显示适当的框架特定属性.
如何注册通过条件共享VCL和FMX代码的组件?
我强烈建议您不要尝试创建特定于框架的属性。我建议改为创建单独的特定于框架的适配器组件,然后您可以根据需要将这些适配器之一分配给您的主要组件,例如:
unit MyComponentUI;
interface
uses
Classes;
type
TMyComponentUIControl = class(TComponent)
public
procedure DoSomethingWithControl; virtual; abstract;
...
end;
implementation
...
end.
Run Code Online (Sandbox Code Playgroud)
unit MyComponentFmxUI;
uses
MyComponentUI,
FMX.TabControl;
type
TMyComponentUIControl_FMXTabControl = class(TMyComponentUIControl)
private
FTabControl: TTabControl;
procedure SetTabControl(Value: TTabControl);
protected
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
public
procedure DoSomethingWithControl; override;
published
property TabControl: TTabControl read FTabControl write SetTabControl;
end;
procedure Register;
implementation
uses
FMX.Controls;
procedure TMyComponentUIControl_FMXTabControl.DoSomethingWithControl;
begin
if FTabControl <> nil then
begin
...
end;
end;
procedure TMyComponentUIControl_FMXTabControl.SetTabControl(Value: TTabControl);
begin
if FTabControl <> Value then
begin
if FTabControl <> nil then FTabControl.RemoveFreeNotification(Self);
FTabControl := Value;
if FTabControl <> nil then FTabControl.FreeNotification(Self);
end;
end;
procedure TMyComponentUIControl_FMXTabControl.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited;
if (Operation = opRemove) and (AComponent = FTabControl) then
FTabControl := nil;
end;
procedure Register;
begin
GroupDescendantsWith(TMyComponentUIControl_FMXTabControl, TControl);
RegisterComponents('My Component', [TMyComponentUIControl_FMXTabControl]);
end;
end.
Run Code Online (Sandbox Code Playgroud)
unit MyComponentVclUI;
interface
uses
MyComponentUI,
Vcl.ComCtrls;
type
TMyComponentUIControl_VCLPageControl = class(TMyComponentUIControl)
private
FPageControl: TPageControl;
procedue SetPageControl(Value: TPageControl);
protected
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
public
procedure DoSomethingWithControl; override;
published
property PageControl: TPageControl read FPageControl write SetPageControl;
end;
procedure Register;
implementation
uses
Vcl.Controls;
procedure TMyComponentUIControl_VCLPageControl.DoSomethingWithControl;
begin
if FPageControl <> nil then
begin
...
end;
end;
procedure TMyComponentUIControl_VCLPageControl.SetPageControl(Value: TPageControl);
begin
if FPageControl <> Value then
begin
if FPageControl <> nil then FPageControl.RemoveFreeNotification(Self);
FPageControl := Value;
if FPageControl <> nil then FPageControl.FreeNotification(Self);
end;
end;
procedure TMyComponentUIControl_VCLPageControl.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited;
if (Operation = opRemove) and (AComponent = FPageControl) then
FPageControl := nil;
end;
procedure Register;
begin
GroupDescendantsWith(TMyComponentUIControl_VCLPageControl, TControl);
RegisterComponents('My Component', [TMyComponentUIControl_VCLPageControl]);
end;
end.
Run Code Online (Sandbox Code Playgroud)
unit MyComponent;
interface
uses
Classes,
MyComponentUI;
type
TMyComponent = class(TComponent)
private
FUIControl: TMyComponentUIControl;
procedure SetUIControl(Value: TMyComponentUIControl);
protected
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
public
procedure DoSomething;
published
property UIControl: TMyComponentUIControl read FUIControl write SetUIControl;
end;
procedure Register;
implementation
procedure TMyComponent.DoSomething;
begin
...
if FUIControl <> nil then
FUIControl.DoSomethingWithControl;
...
end;
procedure TMyComponent.SetUIControl(Value: TMyComponentUIControl);
begin
if FUIControl <> Value then
begin
if FUIControl <> nil then FUIControl.RemoveFreeNotification(Self);
FUIControl := Value;
if FUIControl <> nil then FUIControl.FreeNotification(Self);
end;
end;
procedure TMyComponent.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited;
if (Operation = opRemove) and (AComponent = FUIControl) then
FUIControl := nil;
end;
procedure Register;
begin
RegisterComponents('My Component', [TMyComponent]);
end;
end.
Run Code Online (Sandbox Code Playgroud)
通过使用GroupDescendentsWith()到组中的每个适配器与任一FMX.Controls.TControl或Vcl.Controls.TControl,这允许IDE过滤以基于在父项目中使用的框架设计时的组件:
在VCL表单设计器上,您只会TMyComponentUIControl_VCLPageControl在“工具选项板”中看到
可用。
在FMX表单设计器上,您只会TMyComponentUIControl_FMXTabControl在“工具选项板”中看到可用。
在DataModule Designer上,除非将TDataModule.ClassGroup属性设置为VCL或FMX组,否则将看不到任何一个适配器。然后,您将在“工具面板”中看到适当的适配器。
| 归档时间: |
|
| 查看次数: |
917 次 |
| 最近记录: |