2015-04-01 51 views
0

我試圖用FireMonkey平臺(XE7)創建一個非常簡單的組件(Tgraph)。首先,我創建了兩個新類: 1)TGraph(anchestor type TLayout); 2)TMyPlot1D(anchestor type Tpanel); 我保存了兩個單元並創建了一個名爲「MyPackage」的包。我編譯並安裝在「Samples」頁面中。我打開了一個新的Firemonkey項目,並將TGraph實例拖放到窗體中。一切運作良好。在設計時,我可以看到已定義的組件,並且所有相關單元都可以從主單元看到。相關的代碼是在以下幾點:FireMonkey組件執行錯誤

頭等艙

unit UMyPlot; 

interface 

uses 
System.SysUtils, System.Classes, FMX.Types, 
FMX.Controls, FMX.StdCtrls; 

type 
TMyPlot1D = class(TPanel) 
private 
    { Private declarations } 
protected 
    { Protected declarations } 
public 
    { Public declarations } 
published 
    { Published declarations } 
end; 

procedure Register; 

implementation 

procedure Register; 
begin 
    RegisterComponents('Samples', [TMyPlot1D]); 
end; 

end. 

二等

unit UMyGraph; 

interface 

uses 
System.SysUtils, System.Classes, FMX.Types, FMX.Controls, FMX.Layouts,  
UMyPlot; 

type 
    TMyGraph = class(TLayout) 
    private 
    Plot : TmyPlot1D; 
    public 
    constructor create(Aowner:TComponent); override; 
    end; 

procedure Register; 

implementation 

procedure Register; 
    begin 
    RegisterComponents('Samples', [TMyGraph]); 
    end; 

constructor TMyGraph.create(Aowner: TComponent); 
    begin 
    inherited; 
    Plot := TMyPlot1D.Create(Self); 
    plot.Parent := Self; 
end; 

end. 

,當我嘗試運行我的應用程序的問題所示。 我得到了以下錯誤: 「在模塊Project1.exe在000A51FA。類未找到類TmyPlot1D異常EClassNotFound」。失敗的函數似乎是Application.RealCreateForms。

如果我只拖放TmyPlot1D實例,它在設計時和運行時都會起作用(當然)!

任何想法?

在此先感謝

回答

2

在你TMyGraph.Create要創建一個子對象,情節。這種行爲發生在設計時和運行時。

在運行時沒有問題,但是問題在發生,因爲當您保存設計時,組件的子組件也會流出到FMX文件。

當您運行您的應用程序時,它將表單傳入並嘗試在設計時創建的TMyGraph和TMyPlot1D子對象中進行流式處理,但失敗。即使成功,您也會遇到問題,因爲您將在設計時創建TMyPlot1D,並在運行時創建TMyPlot1D。

您可以通過設置解決這個存儲:=假的,你在設計時創建的任何孩子,所以你創建方法是這樣的:

constructor TMyGraph.Create(Aowner: TComponent); 
begin 
    inherited; 
    Plot := TMyPlot1D.Create(Self); 
    Plot.Parent := Self; 
    Plot.Stored := False; 
end; 

現在我們就來之所以類,如果沒有被流媒體系統讀入。在FMX中,您需要調用RegisterFMXClasses(Classes unit)以使類可以流式傳輸到表單中。你需要把這個初始化部分在你的單位的結束,例如:

initialization 
    RegisterFMXClasses([TMYGraph]); 
end. 
+0

非常感謝。現在看來工作。 – Damiano 2015-04-02 12:34:13

0

這個命令是非常重要的:

Tcomponent.Stored:(最終結束之前。)= TRUE;