Flash Builder。创建动态mxml包实例

时间:2016-03-28 10:51:36

标签: actionscript-3 flash flash-builder

所以,我刚刚开始使用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包组件(signindashboard)。

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

我的一些参考(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.
];

对不起我的英语不好:)祝谢。

1 个答案:

答案 0 :(得分:1)

Flex不要从import语句中编译未使用的包。因此,请确保导入所有必需的包并将其设置为任何变量的类型,以便编译器知道我们将在某处使用该包/类。一个工作的例子可能是

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; 
}