【问题标题】:In Flex, is there something like a 'this' reference for an MXML component?在 Flex 中,是否有类似 MXML 组件的“this”引用?
【发布时间】:2009-06-30 09:02:22
【问题描述】:

我可以像这样编写我想要实现的目标:

<mx:Button id="someButton" click="doRememberButton(someButton)" ... />

但是如果我能写的话会发现它很有帮助(我正在整理一个相当大的 UI):

<mx:Button click="doRememberButton(this)" ... />

现在,明显的问题是“this”不指向 Button,而是指向代码所在文件定义的主要组件(例如 VBox),但如果我有一些参考,这将是一个很大的帮助到“当前”MXML 组件..

有人对此有解决方案吗?谢谢! 汤姆

【问题讨论】:

    标签: apache-flex mxml


    【解决方案1】:

    内联事件处理程序实际上只是包装代码,因此您可以使用事件对象来获取调度程序的详细信息和其他事件信息。像这样:

    <mx:Button click="trace(event.target)" />
    

    在您的情况下,您必须更改事件处理程序的签名,例如:

    private function doRememberButton(event:Event):void
    {
        ...
    }
    

    在 MXML 代码中:

    <mx:Button click="doRememberButton(event)" />
    

    事件类的目标属性是事件的原始调度器。还有一个 currentTarget 属性,它是事件链中的当前目标。这与事件冒泡有关。在Adobe LiveDocs中有更多相关信息

    【讨论】:

    • 或 doRememberButton(event.target) 不需要您更改现有代码。
    【解决方案2】:
    private function doRememberButton(ev: Event) {
    
    //this gives your button
    ev.currentTarget;
    
    
    }
    

    【讨论】:

      【解决方案3】:

      这里有一个更准确的解决方案

      <mx:Button id="someButton" click="doRememberButton(event.currentTarget as Button)"  />
      

      函数:

      private function doRememberButton(thisBtn:Button):void
      {
          ...
      }
      

      就是这样! :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-04
        • 1970-01-01
        • 2010-12-08
        • 1970-01-01
        • 2010-12-23
        • 2011-10-15
        • 2011-02-27
        • 1970-01-01
        相关资源
        最近更新 更多