I am using the Spring4D mobile app demo code to instantiate the MainForm
using the TContainer
with a DelegateTo()
method, as shown below. The code works fine to show the MainForm
.
However, the TEdit
on the MainForm
does not have a blinking caret (cursor) until the button is clicked and then the Edit is focused again, or the entire app is deactivated and then reactivated.
Also, using the commented out line Application.CreateForm(TMainForm, MainForm);
works fine for the blinking caret.
program MyMobileApp;
uses
FMX.Forms,
FMX.Platform,
Spring.Container,
System.StartUpCopy,
uMainForm in 'uMainForm.pas' {MainForm};
{$R *.res}
procedure RegisterServices(const container: TContainer);
begin
container.RegisterType<TMainForm>
.Implements<TMainForm>
.AsSingleton
.DelegateTo(
function: TMainForm
begin
// This forces form creation to be synchronous.
// May or may not be called on certain platforms already by the FMX framework.
// Android will call this prior sending the message but iOS won't.
// May be called multiple times with no harm done.
Application.RealCreateForms;
Application.CreateForm(TMainForm, Result);
// Create instance and assign MainForm
// (would otherwise be done by Application.CreateMainForm)
Application.MainForm := Result;
// And make it visible as this may not be set in the designer and
// no window would be displayed
Application.MainForm.Visible := True;
Result := Result;
end);
container.Build;
end;
procedure ResolveMainForm(const container: TContainer);
begin
{$IFDEF MOBILE}
// Platform service needs to handle that
TMessageManager.DefaultManager.SubscribeToMessage(TApplicationEventMessage,
procedure (const Sender: TObject; const M: TMessage)
begin
if M is TApplicationEventMessage then
case TApplicationEventMessage(M).Value.Event of
TApplicationEvent.FinishedLaunching: container.Resolve<TMainForm>;
end;
end);
{$ELSE}
// This method works perfectly and the blinking caret appears fine in TEdit
//Application.CreateForm(TMainForm, MainForm); // <--- Blinking caret works fine
// This method does not show the blinking caret in the TEdit unless the button is clicked
// or application is deactivated/reactivated
container.Resolve<TMainForm>; // <--- Blinking caret does not work
{$ENDIF}
end;
var
container: TContainer;
begin
container := TContainer.Create;
try
Application.Initialize;
RegisterServices(container);
ResolveMainForm(container);
Application.Run;
finally
container.Free;
end;
ReportMemoryLeaksOnShutdown := True;
end.
uMainForm.pas:
unit uMainForm;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls, FMX.Controls.Presentation, FMX.Edit;
type
TMainForm = class(TForm)
Edit1: TEdit;
Button1: TButton;
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.fmx}
end.
uMainForm.fmx:
object MainForm: TMainForm
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 154
ClientWidth = 227
FormFactor.Width = 320
FormFactor.Height = 480
FormFactor.Devices = [Desktop]
DesignerMasterStyle = 0
object Edit1: TEdit
Touch.InteractiveGestures = [LongTap, DoubleTap]
TabOrder = 0
Position.X = 40.000000000000000000
Position.Y = 16.000000000000000000
end
object Button1: TButton
Position.X = 40.000000000000000000
Position.Y = 48.000000000000000000
TabOrder = 1
Text = 'Button1'
end
end