May 14

Peter在写这个例子的时候说“Not sure if this is helpful to anybody”,其他人不敢说,不过这个例子刚好是我所需要的–我在做的一个解析ActionScript文件的项目中需要用到这个功能。例子中演示了如何利用URLLoaderURLVariables类,从一个扩展文件中读入一定格式的内容(name/value),根据读入的内容随即显示在DataGrid控件中。

下面是完整代码:


Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal" verticalAlign="middle" backgroundColor="white" creationComplete="init()"> 
  3.  
  4.     <mx:Script>
  5.         <![CDATA[
  6.             import mx.collections.ArrayCollection;
  7.  
  8.             [Bindable]
  9.             private var VARIABLES_URL:String = "params.txt";
  10.  
  11.             [Bindable]
  12.             private var arrColl:ArrayCollection;
  13.  
  14.             [Bindable]
  15.             private var paramColl:ArrayCollection;
  16.  
  17.             private var urlReq:URLRequest;
  18.             private var urlLdr:URLLoader;
  19.  
  20.             private function init():void {
  21.                 /* Initialize the two ArrayCollections objects with empty arrays. */
  22.                 arrColl = new ArrayCollection();
  23.                 paramColl = new ArrayCollection();
  24.  
  25.                 /* Initialize the URLRequest object with the URL to the file of name/value pairs. */
  26.                 urlReq = new URLRequest(VARIABLES_URL);
  27.  
  28.                 /* Initialize the URLLoader object, assign the various event listeners, and load the specified URLRequest object. */
  29.                 urlLdr = new URLLoader();
  30.                 urlLdr.addEventListener(Event.COMPLETE, doEvent);
  31.                 urlLdr.addEventListener(Event.OPEN, doEvent);
  32.                 urlLdr.addEventListener(HTTPStatusEvent.HTTP_STATUS, doEvent);
  33.                 urlLdr.addEventListener(IOErrorEvent.IO_ERROR, doEvent);
  34.                 urlLdr.addEventListener(ProgressEvent.PROGRESS, doEvent);
  35.                 urlLdr.addEventListener(SecurityErrorEvent.SECURITY_ERROR, doEvent);
  36.                 urlLdr.load(urlReq);
  37.             }
  38.  
  39.             private function doEvent(evt:Event):void {
  40.                 arrColl.addItem({type:evt.type, idx:arrColl.length+1, eventString:evt.toString()});
  41.  
  42.                 switch (evt.type) {
  43.                     case Event.COMPLETE:
  44.                         /* If the load was successful, create a URLVariables object from the URLLoader.data property and populate the paramColl ArrayCollection object. */
  45.                         var ldr:URLLoader = evt.currentTarget as URLLoader;
  46.                         var vars:URLVariables = new URLVariables(ldr.data);
  47.                         var key:String;
  48.  
  49.                         for (key in vars) {
  50.                             paramColl.addItem({key:key, value:vars[key]});
  51.                         }
  52.  
  53.                         params.visible = true;
  54.                         break;
  55.                 }
  56.             }
  57.         ]]>
  58.     </mx:Script> 
  59.  
  60.     <mx:VBox>
  61.         <mx:Label text="Events:" />
  62.         <mx:DataGrid id="events" dataProvider="{arrColl}" rowCount="5">
  63.             <mx:columns>
  64.                 <mx:DataGridColumn dataField="idx" headerText="#" width="20" />
  65.                 <mx:DataGridColumn dataField="type" headerText="Type" showDataTips="true" dataTipField="eventString" />
  66.             </mx:columns>
  67.         </mx:DataGrid>
  68.     </mx:VBox> 
  69.  
  70.     <mx:VBox>
  71.         <mx:Label text="Parameters:" />
  72.         <mx:DataGrid id="params" dataProvider="{paramColl}" rowCount="5" visible="false">
  73.             <mx:columns>
  74.                 <mx:DataGridColumn dataField="key" headerText="Key" />
  75.                 <mx:DataGridColumn dataField="value" headerText="Value" />
  76.             </mx:columns>
  77.         </mx:DataGrid>
  78.     </mx:VBox> 
  79.  
  80. </mx:Application>
代码:Peter deHaan 翻译/整理/编译:minidxer

written by Minidxer  |  tags: , , , ,

Related Post

One Response to “Flex中利用URLLoader和URLVariables类导入文件的例子”

Trackbacks

Leave a Reply