Flex-动态加载SWF文件并获取对象类

本文档提供了一个使用Flex的详细示例,展示了如何在应用程序中加载外部SWF文件并获取类定义。通过实例代码,作者解释了如何处理加载过程中的各种事件,如加载完成、I/O错误和安全错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

示例
关键代码:
  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.fxug.net/
 10      LINKcom : http://www.linkcom.co.jp/ (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 

转载于:https://www.cnblogs.com/jssy/archive/2007/09/09/887815.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值