Flex-动态加载SWF文件并获取对象类示例
关键代码: 1 <?xml version="1.0" encoding="utf-8"?> 2 <!-- 3 4 [Pepe's Samples] part 1 5 6 Author : Pepe at Flex User Group in Japan (FxUG). 7 an architect at LINKcom corporation. 8 Blog : http://Shigeru-Nakagaki.com/ 9 FxUG : http://www./ 10 LINKcom : http://www./ (Japanese only) 11 12 Instructions 13 You can use this sample code in your own responsibility. 14 15 --> 16 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" viewSourceURL="srcview/index.html"> 17 18 <mx:creationComplete> 19 <![CDATA[ 20 initApp(); 21 ]]> 22 </mx:creationComplete> 23 24 <mx:Script> 25 <![CDATA[ 26 import flash.utils.describeType; 27 import mx.controls.Alert; 28 29 30 private var ld:Loader; 31 32 private function initApp():void 33 { 34 ld = new Loader(); 35 ld.contentLoaderInfo.addEventListener(Event.COMPLETE,loadCompleted); 36 ld.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioErrHandler); 37 ld.contentLoaderInfo.addEventListener( SecurityErrorEvent.SECURITY_ERROR,securityErrHandler); 38 } 39 40 private function loadSWF():void 41 { 42 var sSWF:String = txtURL.text; 43 var req:URLRequest = new URLRequest(sSWF); 44 var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain,null); 45 req.contentType = ""; 46 ld.load(req,context); 47 } 48 49 private function getDef():void 50 { 51 var cls:Class = loadClassDefinition(txtClassName.text); 52 if(cls){ 53 var obj:Object = new cls(); 54 txtDump.text = flash.utils.describeType(obj).toString(); 55 } 56 } 57 58 private function loadCompleted(e:Event):void 59 { 60 Alert.show("[" + e.target.url + "] loaded"); 61 } 62 63 private function ioErrHandler(e:IOErrorEvent):void 64 { 65 Alert.show(e.text); 66 } 67 68 private function securityErrHandler(e:IOErrorEvent):void 69 { 70 Alert.show(e.text); 71 } 72 73 private function loadClassDefinition(sClassName:String):Class 74 { 75 var cls:Class = null; 76 try{ 77 cls = ld.contentLoaderInfo.applicationDomain. getDefinition(sClassName) as Class; 78 return cls; 79 }catch(e:Error){ 80 throw new IllegalOperationError(sClassName + " doesn't exist"); 81 } 82 return cls; 83 } 84 85 86 ]]> 87 </mx:Script> 88 89 <mx:Label text="A sample of describing class definition " fontWeight="bold" fontSize="16"/> 90 <mx:HRule width="100%" height="40" /> 91 <mx:HBox width="100%"> 92 <mx:TextInput id="txtURL" text= "http://Shigeru-Nakagaki.com/flex_samples/ApplicationDomain/getDefinitionDump /RunTimeClass03/RunTimeClass03.swf" width="400" /> 93 <mx:Button x="10" y="10" label="Load SWF" click="loadSWF()"/> 94 </mx:HBox> 95 <mx:HBox width="100%"> 96 <mx:TextInput id="txtClassName" text="org.Pepe.ExPanel" width="400" /> 97 <mx:Button x="10" y="40" label="Get Definition" click="getDef()"/> 98 </mx:HBox> 99 <mx:HBox width="100%" height="90%"> 100 <mx:TextArea id="txtDump" width="100%" height="100%"/> 101 </mx:HBox> 102 103 </mx:Application> 104 105 |
|