Flex反射:通过调用getDefintionByName函数动态创建按钮等控件的例子

flex下的flash.utils类中包含了反射用到的方法,反射就是在运行时动态的得到的类的信息或者动态的创建类,而不是在程序编译时就new好的,flex中的反射还不是很健全,如果要反射的类是flex自带的,那么运行时没啥问题,如果是自己定义的,就必须在程序某处引用一下,否则会报错“Variable … is not defined”, because the Flash Player can’t find it at run time。

解决办法,在外国网站找到的

1. You can import the class and create a dummy variable. The dummy is needed, otherwise the compiler won’t add the class.
import domain.project.MyClass;
...
private var _dummyClass:MyClass;

2. Instead of a variable you can also use the following notation.
import domain.project.MyClass;
...
MyClass;

3. You can also use the Frame metadata tag:
import domain.project.MyClass;
...
[Frame(extraClass="MyClass")]

All these solutions work, but you need to specify the classes you want to use up front.

4. Far more easy, elegant and flexible is to include the path of the class when calling getDefinitionByName. You won’t need to import anything and you don’t have to create dummy vars or use extra metadata tags.
var myClass = getDefinitionByName("domain.project.MyClass") as Class;

接下来的例子演示了ActionScript 3.0中通过调用getDefintionByName函数,动态创建按钮等控件。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值