【问题标题】:Pass parameter to Complete Event - Adobe Flex将参数传递给完成事件 - Adob​​e Flex
【发布时间】:2011-07-29 06:21:08
【问题描述】:

我正在使用 HTTP 请求以二进制格式下载图像。下载完成后,我想处理它,但我也想将图像的ID传递给完整的处理函数......这是怎么做的?

var loader:URLLoader = new URLLoader();

for(var i:int = 0 ; i<5; i++){

    /* When completed I want to access the variable "i" */
    loader.addEventListener(Event.complete, completeHandler);
    loader.load(/* a url request */);
}

private function completeHandler(event:Event):void
{
     /* I want to access the passed parameter "i" so 
      it is the same as it was when the eventListener was added, 0,1,2,3 or 4 */

 }

这可能吗?我试过扩展事件,但我想处理 COMPLETE 事件

谢谢 菲尔

【问题讨论】:

    标签: apache-flex actionscript adobe


    【解决方案1】:

    exacly - 附加方法 addArguments(...) 是最好的解决方案,我使用相同的方法,但它调用 passParameters

    public function passParameters(method:Function,additionalArguments:Array):Function
    {return function(event:Event):void{
        method.apply(null, [event].concat(additionalArguments));}
    }
    

    对此的解释在这里 - 它简单且始终有效http://sinfinity.pl/blog/2012/03/28/adding-parameters-to-event-listener-in-flex-air-as3/

    【讨论】:

      【解决方案2】:

      这应该可以使用 Flex 的动态函数构造来实现。 herehere 也提出了类似的问题。

      这是一个例子:

      参数和处理程序:

      var parameters:String = "Some parameter I want to pass";
      
      private function loadLocalData(e:Event, parameter:String):void
      {
        // voila, here's your parameter
      }
      
      private function addArguments(method:Function, additionalArguments:Array):Function 
      {
        return function(event:Event):void {method.apply(null, [event].concat(additionalArguments));}
      }
      

      示例中的用法:

      for(var i:int = 0 ; i<5; i++){
      
          /* When completed I want to access the variable "i" */
          loader.addEventListener(Event.complete, addArguments(completeHandler, [i]));
          loader.load(/* a url request */);
      }
      
      private function completeHandler(event:Event, id:int):void
      {
           /* I want to access the passed parameter "i" so 
            it is the same as it was when the eventListener was added, 0,1,2,3 or 4 */
      
      }
      

      【讨论】:

      • 谢谢!那么当我在 completeHandler(e:Event) 中时,我如何访问“i”?
      • @Phil, i 将作为completeHandler 函数中的id 参数传递。您可以使用id 访问它。当然,如果您愿意,可以将id 更改为i。 :)
      猜你喜欢
      • 2015-07-23
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多