i trying initialize form properties in program source file in fire monkey application , throws exception. here code:
uses system.startupcopy, fmx.forms, umainform in 'units\umainform.pas' {mainform}, udatamodule in 'units\udatamodule.pas' {datamod: tdatamodule}, datahelperclasses in 'units\datahelperclasses.pas', exdintf in 'units\exdintf.pas', exd in 'units\exd.pas'; {$r *.res} var viewmodel: texdviewmodel; begin application.initialize; application.createform(tdatamod, datamod); application.createform(tmainform, mainform); viewmodel := texdviewmodel.create; mainform.data := datamod; mainform.viewmodel := viewmodel; //this throws access violation exception viewmodel.data := datamod; application.run; end.
i have no problem doing in vcl app. how fix it?
there difference in behavior between vcl , fmx - firemonkey application.createform
method. while in vcl createform
creates form , after call form variable initialized , ready used, in fmx createform
not create form , form variable still uninitialized - nil
- after call. because of using form variable throws av.
createform not create given form immediately. adds request pending list. realcreateforms creates real forms.
fmx has application.realcreateforms
method automatically called in application.run
. if need use form variables before that, can call application.realcreateforms
yourself. after call can safely use form variables added list application.createform
keep in mind application.realcreateforms
go through form creation process once, have call after made all calls application.createform
or end unitialized forms.
begin application.initialize; application.createform(tdatamod, datamod); application.createform(tmainform, mainform); // forces creation of firemonkey forms application.realcreateforms; ....
note: on windows , osx platforms realcreateforms
first thing called in application.run
, not matter whether called or automatically. however, on android , ios platforms additional (initialization) logic happens before realcreateforms
called in application.run
, if develop platforms, should proceed caution when using realcreateforms
, watch potential side-effects. best option mobile platforms move custom initialization form oncreate
event.
Comments
Post a Comment