2010-09-05 52 views
1

我在我的应用程序中有一个VGroup,其中包含MyComponent类型的几个自定义组件。现在我在MyComponent中有一个删除方法,它应该从VGroup中删除元素。我知道我应该使用parentDocument.vgroupId.removeElement(),但是要传递什么作为参考?Flex:删除可视元素

注:我想从MyComponent

UPDATE做的方法中删除:这里是我的源: 在我的主要应用

<s:VGroup id="vgroupId" width="100%" height="100%" /> 

现在添加我的自定义组件:

var cust:FunctionElement = new MyComponent(); // MyComponent extends spark Panel 
vgroupId.addElement(cust); 

而从MyComponent我打电话给

parentDocument.vgroupId.removeElement(this) // get this error => TypeError: Error #1034: Type Coercion failed: cannot convert [email protected] to mx.core.IVisualElement. 

如果我投它作为this as IVisualElement我得到一个错误,它等于null

+0

我只注意到你正在添加MyComponent作为FunctionElement ..为什么? – ocodo 2010-09-06 01:24:02

回答

1

这个例子会告诉你如何能做到这一点..

ExampleApp.mxml

<?xml version="1.0" encoding="utf-8"?> 
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
       xmlns:s="library://ns.adobe.com/flex/spark" 
       xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">  
    <fx:Script> 
     <![CDATA[ 
      protected function button1_mouseUpHandler(event:MouseEvent):void 
      { 
       vgroup.addElement(new MyComponent()); 
      } 
     ]]> 
    </fx:Script> 
    <s:VGroup id="vgroup" top="30" /> 
    <s:Button label="Add" mouseUp="button1_mouseUpHandler(event)"/> 
</s:Application> 

像这样定义MyComponent.mxml ...

<?xml version="1.0" encoding="utf-8"?> 
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
     xmlns:s="library://ns.adobe.com/flex/spark" 
     xmlns:mx="library://ns.adobe.com/flex/mx" width="100" height="35"> 
    <fx:Script> 
     <![CDATA[ 
      import spark.components.VGroup; 
      protected function button1_mouseUpHandler(event:MouseEvent):void 
      { 
       // A few useful traces to see what's what and where. 
       trace(this); 
       trace(this.parent); 
       trace((this.parent as VGroup).getElementIndex(this)); 
       // But all we actually need is ... 
       var vgroup:VGroup = (this.parent as VGroup); 
       vgroup.removeElement(this); 
       // (this.parent as VGroup).removeElement(this); // Would also work fine. 
      } 
     ]]> 
    </fx:Script> 
    <s:Button mouseUp="button1_mouseUpHandler(event)" label="Kill me!"/> 
</s:Group> 
+0

已经尝试过。并获取错误“ArgumentError:在此组中找不到null”。 – Adnan 2010-09-05 09:32:54

+0

parentDocument.vgroupId.getElementIndex(this as IVisualElement)返回什么?你可以发布信息来源吗? – ocodo 2010-09-05 11:09:30

+0

@slomojo刚刚更新了源代码的问题。 – Adnan 2010-09-05 11:21:43

0
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:s="library://ns.adobe.com/flex/spark"> 
    <mx:Script> 

      import mx.core.IVisualElement; 
      import spark.components.Button; 

      private function getNewElement():IVisualElement 
      { 
       var btn:spark.components.Button = new spark.components.Button(); 
       btn.label = "button " + myContent.numElements; 
       return btn; 
      } 

      private function addFirstElement():void 
      { 
       myContent.addElementAt(getNewElement(), 0); 
      } 

      private function removeFirstElement():void 
      { 
       if(myContent.numElements > 0) 
        myContent.removeElement(myContent.getElementAt(0)); 
      } 

      private function removeLastElement():void 
      { 
       if(myContent.numElements > 0) 
        myContent.removeElementAt(myContent.numElements - 1); 
      } 


    </mx:Script> 


    <mx:Button label="addFirst" click="addFirstElement();" /> 
    <mx:Button label="removeFirst" click="removeFirstElement()" /> 
    <mx:Button label="removeLast" click="removeLastElement()" /> 
    <mx:Button label="removeAll" click="myContent.removeAllElements()" /> 

    <mx:VBox id="myContent"> 
    </mx:VBox> 

</mx:Application>