Flex反射:通过调用getDefintionByName函数动态创建按钮等控件的例子
2010-08-30 16:44
609 查看
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函数,动态创建按钮等控件。
解决办法,在外国网站找到的
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函数,动态创建按钮等控件。
<?xml version="1.0" encoding="utf-8"?> <mx:Application name="getDefinitionByName_test" xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white" viewSourceURL="srcview/index.html"> <mx:Script> <!--[CDATA[ import mx.core.UIComponent; import flash.utils.getDefinitionByName; import mx.controls.*; /** * Create references to component classes so the classes get * included in the SWF document. */ private var dummyArr:Array = [Button, CheckBox, ComboBox, List, TextInput, TextArea]; private function createBtn_click(evt:MouseEvent):void { var className:String = comboBox.selectedItem.toString(); // Convert class name to Class object. var cls:Class = getDefinitionByName(className) as Class; // Create a new instance of the class. var instance:UIComponent = new cls(); if (instance) { switch (instance.className) { case "Button": Button(instance).label = "I am a Button"; break; case "CheckBox": CheckBox(instance).label = "I am a CheckBox"; break; case "ComboBox": ComboBox(instance).dataProvider = ["I am a ComboBox"]; break; case "List": List(instance).dataProvider = ["I am a List"]; instance.width = 100; break; case "TextInput": TextInput(instance).text = "I am a TextInput"; break; case "TextArea": TextArea(instance).text = "I am a TextArea"; break; } // Remove all children and add new child. canvas.removeAllChildren(); canvas.addChild(instance); } } ]]--> </mx:Script> <mx:ApplicationControlBar dock="true"> <mx:Form styleName="plain" defaultButton="{createBtn}"> <mx:FormItem label="Class:" direction="horizontal"> <mx:ComboBox id="comboBox"> <mx:dataProvider> <mx:Array> <mx:String>mx.controls.Button</mx:String> <mx:String>mx.controls.CheckBox</mx:String> <mx:String>mx.controls.ComboBox</mx:String> <mx:String>mx.controls.List</mx:String> <mx:String>mx.controls.TextInput</mx:String> <mx:String>mx.controls.TextArea</mx:String> </mx:Array> </mx:dataProvider> </mx:ComboBox> <mx:Button id="createBtn" label="Create" click="createBtn_click(event);" /> </mx:FormItem> </mx:Form> </mx:ApplicationControlBar> <mx:Canvas id="canvas" horizontalCenter="0" verticalCenter="0" /> </mx:Application>
相关文章推荐
- C#通过反射来动态创建类对象并调用相关泛型函数(Excel数据自动导入到SQL Server数据库)
- Flex 中用类名反射出类实例(getDefinitionByName)
- Flex中如何通过设定headerStyleName样式在Accordion控件中使用嵌入字体的例子
- javassist学习:动态创建二进制Java类二进制字节码并通过反射调用的示例
- Flex中通过textAlign样式在一个按钮(Button)控件中设置标签(label)对齐方式的例子
- 【转】Flex 中用类名反射出类实例(getDefinitionByName)
- (44)通过继承创建线程对象的例子+getName+setName+currentThread()方法应用介绍
- 通过GetProcAddress函数动态调用dll中地函数,是否必须通过extern C声明导出函数?
- C#.NET dll封装(包含UI和实现函数),通过反射调用dll实现控件的快速加载(PrimPointList.cs)
- Delphi 使用 RTTI 动态通过名称调用函数和设置控件属性值
- 通过GetProcAddress函数动态调用dll中地函数,是否必须通过extern C声明导出函数?
- C#.NET dll封装(包含UI和实现函数),通过反射调用dll实现控件的快速加载(PrimPointListCreator.cs)
- 通过GetProcAddress函数动态调用dll中地函数,是否必须通过extern C声明导出函数?(转)
- 通过反射动态创建了一个控件,如何再用发射通过该控件的名字得到该控件呢?
- C#.NET dll封装(包含UI和实现函数),通过反射调用dll实现控件的快速加载(PointListTypeAttributes.cs)
- 通过反射动态调用set,get方法
- [VB.NET]反射问题,怎么通过动态创建的控件得到控件的方法!
- Flex中通过设置textAlign和headerStyleName样式设置DateChooser控件头部年月文本对齐方式的例子
- getDefinitionByName动态创建类。
- Flex中通过设置fontFamily和labelStyleName样式在HSlider控件中使用嵌入字体的例子