I’m trying to create my own custom calendar component. My idea is for this component to override getstyleobject and have its own default custom style and change some other behavior.
The problem I have is that when I try to create a custom component that inherits from TCalendar
I can’t get it to display correctly. In my implementation I had a .rc
file which pointed to a .style
file which I tried to link by overriding GetStyleObject
.
But even if I don’t do this and keep the custom calendar component code as simple as possible:
unit MyCalendar;
interface
uses
FMX.Calendar;
type
[ComponentPlatformsAttribute(pidAllPlatforms)]
TMyCalendar = class(TCalendar);
procedure Register;
implementation
uses
System.Classes;
procedure Register;
begin
RegisterComponents('My Controls', [TMyCalendar]);
end;
initialization
RegisterClass(TMyCalendar);
finalization
UnRegisterClass(TMyCalendar);
end.
It displays like this:
I have other custom components that inherit from fmx controls and they behave fine so I don’t understand why the calendar isn’t working.
Figured it out. TCalendar
uses a presentation class TStyledCalendar
in FMX.Calendar.Style
and it seems my new calendar class TMyCalendar
needs to register the presentation class as well. It isn’t done so automatically.
So we need to this
initialization
TPresentationProxyFactory.Current.Register(TMyCalendar, TControlType.Styled, TStyledPresentationProxy<TStyledCalendar>);
finalization
TPresentationProxyFactory.Current.Unregister(TMyCalendar, TControlType.Styled, TStyledPresentationProxy<TStyledCalendar>);
Full code:
unit MyCalendar;
interface
uses
FMX.Calendar;
type
[ComponentPlatformsAttribute(pidAllPlatforms)]
TMyCalendar = class(TCalendar);
procedure Register;
implementation
uses
System.Classes,
FMX.Controls.Presentation,
FMX.Presentation.Factory,
FMX.Calendar.Style,
FMX.Controls,
FMX.Presentation.Style;
procedure Register;
begin
RegisterComponents('My Controls', [TMyCalendar]);
end;
initialization
TPresentationProxyFactory.Current.Register(TMyCalendar, TControlType.Styled, TStyledPresentationProxy<TStyledCalendar>);
finalization
TPresentationProxyFactory.Current.Unregister(TMyCalendar, TControlType.Styled, TStyledPresentationProxy<TStyledCalendar>);
end.