您的位置:首页 > 其它

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函数,动态创建按钮等控件。



<?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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐