Flex:在parentDocument上调用时,方法不起作用

时间:2010-05-08 16:54:07

标签: flex

我想知道是否有人可以查看此代码并告诉我为什么在从同一文档调用时调用removeSelectedChild,但在从子文档/组件调用时返回以下错误。

“ArgumentError:错误#2025:提供的DisplayObject必须是调用者的子级。”

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" >

  <mx:Accordion id="myAccordion" 
  width="100%" height="100%"
  selectedIndex="0">

  <mx:Script>
    <![CDATA[

      public function removeSelectedChild():void {
          trace("before");
      try {
              myAccordion.selectedChild.parent.removeChild(myAccordion.selectedChild)
      } catch(err:ReferenceError) {
          trace("catch") 
          }
          trace("after");
      }

    ]]>

  </mx:Script>

  <mx:headerRenderer>
    <mx:Component>
      <mx:Button click="{ parentDocument.removeSelectedChild() }" />
    </mx:Component>
  </mx:headerRenderer>

  <mx:HBox>
    <mx:Button click="{ removeSelectedChild() }" />
  </mx:HBox>

  </mx:Accordion>

</mx:Application>

单击子项中的按钮会产生预期的结果,而单击标题会删除子项,但也会抛出错误,尽管它们都调用完全相同的方法。

很抱歉,这个例子有点人为,这个问题出现在一个非常复杂的视图中,它使用了各种自定义组件。这是我能够以一种快速编制方式显示它的唯一方式,并且可以在没有背景噪音的情况下轻松关注真实问题。

我正在把头发拉出来,如果有人能提供帮助,我会非常感激。

更新:我现在发现在方法执行完毕后触发了异常。请参阅上面的跟踪语句。在抛出异常之前跟踪“after”。

干杯,

克里斯

3 个答案:

答案 0 :(得分:0)

不是解决方案,但也许是一种解决方法:您可以尝试重写这样的方法:

myAccordion.selectedChild.parent.removeChild(myAccordion.selectedChild)

如果不起作用,Flex中必定存在错误。

答案 1 :(得分:0)

使用“outerDocument”代替“parentDocument”。定义了一个新的小范围。

答案 2 :(得分:0)

如您所述,您的函数不会抛出错误。单击手风琴标题时的默认操作是展开或收缩该元素。由于您要删除该元素,因此当Flex尝试展开或收缩它时,您会收到错误。

您可以通过停止执行默认操作来修复它:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" >

    <mx:Accordion id="myAccordion" 
                  width="100%" height="100%"
                  selectedIndex="0">

        <mx:Script>
            <![CDATA[

                public function removeSelectedChild(event:MouseEvent):void {
                    myAccordion.removeChild(myAccordion.selectedChild);
                    event.stopImmediatePropagation();
                }

            ]]>

        </mx:Script>

        <mx:headerRenderer>
            <mx:Component>
                <mx:Button click="{ parentDocument.removeSelectedChild(event) }" />
            </mx:Component>
        </mx:headerRenderer>

        <mx:HBox>
            <mx:Button click="{ removeSelectedChild(event) }" />
        </mx:HBox>

    </mx:Accordion>

</mx:Application>
相关问题