【问题标题】:how can I use the event parameter when the code is written inline in the mxml tag in flexflex中的mxml标签内联编写代码时如何使用事件参数
【发布时间】:2011-06-02 07:40:15
【问题描述】:

有什么区别:

在 HTTPService 类的 result 属性中提供函数引用,如下所示:

protected function httpserviceCheckUpdate_resultHandler(event:ResultEvent):void
{
     var version:Number = event.result.text.version as Number;
     var xmldata:XML = XML(event.result);
     // and all the processing you want to do
}
<mx:HTTPService id="httpserviceCheckUpdate"
                    method="POST"
                    url="http://abc.com/actions.php"
                    resultFormat="text"
                    fault="httpservicexml_faultHandler(event)"
                    result="httpserviceCheckUpdate_resultHandler(event)">
        <mx:request xmlns="">
            <action>versionCheck</action>
        </mx:request>
    </mx:HTTPService>

在同一个 HTTPService 类的&lt;mx:result&gt;&lt;/mx:result&gt; 中编码完整的函数。

<mx:HTTPService id="httpserviceCheckUpdate"
                method="POST"
                url="http://abc.com/actions.php"
                resultFormat="text"
                fault="httpservicexml_faultHandler(event)"
                result="httpserviceCheckUpdate_resultHandler(event)">
    <mx:request xmlns="">
        <action>versionCheck</action>
    </mx:request>
    <mx:result>
        <![CDATA[
        var version:Number = event.result.text.version as Number;
        var xmldata:XML = XML(event.result);
        // and all the processing you want to do
        ]]>
    </mx:result>
</mx:HTTPService>

如果它们都相同,那么如何使用第二种方法提供事件参数(如 httpservice 类的结果属性中所写)?

在 FLEX 中哪一个是更好的处理方式

谢谢

【问题讨论】:

    标签: apache-flex mxml


    【解决方案1】:

    使用&lt;mx:result&gt; 标签不是标准的做事方式。您可以使用 MXML 对任何事件使用标签。它是为快速原型设计而不是代码清洁而设计的。它们并不完全相同,因为后者是一个没有参数的匿名函数,但是您可以使用 httpserviceCheckUpdate.lastResult 属性访问结果。

    您的第一种方法更好,因为它是标准的、更简洁、可重复使用并且您可以设置参数。

    【讨论】:

      【解决方案2】:

      实际上我主要在代码中设置事件,所以甚至不像你的第一个示例,而是在一个完整的事件调用中:

      component.addEventListener(<event>, <handler>, <usecapture>, <priority>, <weakreference>)
      

      始终使用全部 5 个参数。我在代码中执行此操作,因为我倾向于在不需要侦听器时删除它们,然后在需要时再次添加它们:这只能在代码中完成。

      我总是使用所有 5 个参数,因为它可以很好地控制优先级(谁首先获得事件),其次是因为内存管理:如果我忘记删除侦听器并且 eventdispatcher 是唯一的强参考对于事件处理程序,垃圾收集器仍然能够删除处理程序所属的对象。我认为仅此一项就值得在代码中定义事件处理程序。

      回到你的问题:我从来没有找到像你这样的例子,我发现是这样的:

      <mx:HTTPService id="httpserviceCheckUpdate"
                      method="POST"
                      url="http://abc.com/actions.php"
                      resultFormat="text"
                      fault="httpservicexml_faultHandler(event)"
                      result="httpserviceCheckUpdate_resultHandler(event)">
          <mx:request xmlns="">
              <action>versionCheck</action>
          </mx:request>
          <mx:result>
              <![CDATA[
              import ....ResultEvent;
              protected function httpserviceCheckUpdate_resultHandler(event:ResultEvent):void 
              {
                  var version:Number = event.result.text.version as Number;
                  var xmldata:XML = XML(event.result);
                  // and all the processing you want to do
              }
              ]]>
          </mx:result>
      </mx:HTTPService>
      

      如您所见,您有一个完整的函数签名,包括此处的事件。唯一的区别是组件中定义的处理程序将在容器中定义的处理程序之前被调用,除了我认为它们同样好,但都比在代码中设置处理程序更差。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-22
        • 2019-11-30
        • 1970-01-01
        • 1970-01-01
        • 2011-11-05
        • 1970-01-01
        • 2023-03-20
        • 1970-01-01
        相关资源
        最近更新 更多