2016-02-07 36 views
1

我正在通過VCL組件移植到FMX。 99%的代碼是純粹的對象pascal,所以工作得很好 - 但我有一個創建表單的方法,用按鈕和文本框來填充它,而這在FMX下是不起作用的。創建Firemonkey表單並通過代碼填充

手動創建表單然後從代碼填充它的關鍵是確保它在VCL,LCL和FMX下編譯;並且它在iOS,Android和任何平臺使用下也顯示得很好。

但我不斷收到「資源/類名/找不到」,其中/classname/是我給我的臨時表單類的任何類名。

事情如此簡單產生錯誤:

type 
TMyDialogForm = Class(TForm); 

procedure TForm1.Button1Click(Sender: TObject); 
var 
    LDialog: TMyDialogForm; 
begin 
    LDialog := TMyDialogForm.Create(application.MainForm); 
    try 
    LDialog.Caption := 'Yahoo!'; 
    finally 
    LDialog.Free; 
    end; 
end; 

由於錯誤涉及資源,我懷疑它是尋找某種類型的佈局數據。我剛開始玩FMX,我注意到不同的平臺允許不同的佈局。但我必須承認,無論您的目標是什麼平臺,我都希望它回落到默認主題。

那麼 - 我如何通過代碼創建表單,填充它並使用Firemonkey顯示ut而不會遇到這種錯誤?它在VCL和LCL下工作得很好,但FMX一直在關注資源。

請不要告訴我所有的形式必須被設計?

回答

4

由於@RemyLebeau回答在Delphi論壇類似的問題(How to create a TForm at runtime?):

You are calling the TForm constructor that invokes DFM streaming. The reason it does not fail in non-FMX apps is because TCustomForm.Create() filters out TForm specifically so it won't try to stream. In FMX, TCommonCustomForm.Create() filters out TCommonCustomForm instead of TForm, which is why your TForm in FMX is trying to stream itself.

Since you know that there is no DFM, you should be using the non-DFM constructor instead, in both VCL and FMX:

FRM := TForm.CreateNew(Application);