所以,我剛開始Flash構建器。給點Flash Builder。創建動態mxml包實例
我的應用程序包結構:
index.mxml (default) <s:application/>
[main]
signin.mxml <s:form/>
dashboard.mxml <s:group/>
[content]
home.mxml
data_a.mxml
data_b.mxml
post.mxml
and-many-more-mxml-file.mxml
etc.mxml
index.mxml
工作的(像)前端控制器。那隻會加載main
封裝組件(signin
,dashboard
)。
index.mxml fx:script
:
import main.signin;
import main.dashboard;
public var m_signin:main.signin = new signin();
public var m_dashboard:main.dashboard = new dashboard();
private function app_ready():void { //creationComplete
//...service to check session...
if(is_signed) {
addElement(m_dashboard);
} else {
addElement(m_signin);
}
}
成功。
我們假設用戶已簽名。然後從main/dashboard.mxml
我想從數組中定義的main.content.*
加載包。
主/ dashboard.mxml fx:script
:
protected var dashboard_content:Object = {};
protected var dashboard_content_list:Array = [
['home','Home Admin'],
['user_a','User Data A'],
['user_b','User Data B']
//many more.
];
for (var i:int = 0; i < dashboard_content_list.length; i++) {
var content_class:Class = Class(getDefinitionByName('main.content.'+dashboard_content_list[i][0]));
var content_obj:Object = new content_class();
dashboard_content[dashboard_content_list[i][0]] = content_obj; //for refference each component
}
每個陣列從dashboard_content_list
解釋:
0: package suffix that i want to load (ex: `main.content.home`).
1: button label (for each content).
但該循環仍然沒有工作,總是ReferenceError: Error #1065
。
所以我想要的待辦事項是: 加載main.content.*
包類,並將其保存在Object
在main/dashboard.mxml
。
我的一些REFFERENCE(but still not working.
)的: http://www.untoldentertainment.com/blog/2008/11/12/as3-tutorials-instantiate-a-class-dynamically/ http://blogs.adobe.com/cantrell/archives/2010/09/loading-classes-dynamically-in-actionscript-3.html http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/package.html#getQualifiedSuperclassName() Instantiate a class from a string in ActionScript 3 Dynamically Creating Flex Components In ActionScript AS3: Cast Variable as Dynamic Type https://delfeld.wordpress.com/2009/04/21/object_from_class_name/ http://www.mikechambers.com/blog/2006/06/22/actionscript-3-get-a-class-reference-by-class-name/ http://www.rozengain.com/blog/2009/08/21/getdefinitionbyname-referenceerror-and-the-frame-metadata-tag/
respons到@SameerJain
我有嘗試:
import main.content.*;
//inside creationComplete
var c:Class = getDefinitionByName('main.content.home') as Class; // throw: ReferenceError: Error #1065
var o:Object = new c();
trace(o);
然後,我嘗試:
var c:Class = getDefinitionByName(getQualifiedClassName('main.content.home')) as Class;
var o:Object = new c();
trace(c); // return [class String]
trace(o); // return '' (empty)
trace(main.content.home); // return [class home] <<< this what I want.
解決
所以,@ SameerJain的回答引導我解決我的問題。
protected var dashboard_content_list:Array = [
/*
index-0: the class inself <<< this solved my problem!
index-1: the label button
*/
[main.content.home,'Home Admin'],
[main.content.user_a,'User Data A'],
[main.content.user_b,'User Data B']
//many more.
];
對不起,我的英文不好:)
謝謝。
你要做的是使用一個Flex項目,並嘗試使它像一個純動作項目一樣工作。這不會奏效,要麼將其製作爲動作項目,要麼利用Flex SDK而不是試圖繞過它。 Flex SDK確實爲您管理類的創建,或者讓sdk管理.mxml的創建,或者如果您想成爲那樣做的人,那麼這會讓他們成爲純粹的.as類。 – BotMaster
恩,好吧。任何例子? – anovsiradj