闪存生成器。创建动态 mxml 包实例

Flash Builder. Create dynamic mxml package instance

所以,我刚刚开始使用 Flash Builder。到点

我的应用包结构:

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.* 加载包。

main/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.* 包 class 并将其保存在 main/dashboard.mxml 中的 Object

我的一些参考(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/

回复@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 不会从您的导入语句中编译未使用的包。因此,请确保导入所有必需的包并将其设置为任何变量的类型,以便编译器知道我们将在某处使用 package/class。一个工作示例可以是

import main.content.*;

private var dashboardhome:home;
private var dashboarduser_b:user_a;
private var dashboarduser_a:user_a;

for (var i:int = 0; i < dashboard_content_list.length; i++) {
    var content_class:Class = getDefinitionByName('main.content.'+dashboard_content_list[i][0]) as Class;
    var content_obj:Object = new content_class();
    dashboard_content[dashboard_content_list[i][0]] = content_obj; 
}