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. 2. Instead of a variable you can also use the following notation. 3. You can also use the Frame metadata tag: 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. 例子如下: <?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> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- Swift-->Json解析(原生NSJSONSerialization)
- $.ajax json数据到server,报400错误的解决方案
- reactjs – 如何使用React ES6 webpack导入和导出组件?
- 使用Reactor 2.0在Spring 4上执行多线程执行
- 关于xml文件在浏览器报错的问题
- 用正则表达式(regex_replace)模拟读取INI文件
- ios – 在UITableView中动态单元格中的Swift-Access UILabe
- 详解vue slot插槽的使用方法
- Sqlite3 ---------------用法
- 程序缘 Flex AIR之旅--第六章 编写用户登录界面