代码之家  ›  专栏  ›  技术社区  ›  Adnan

flex:删除可视元素

  •  1
  • Adnan  · 技术社区  · 14 年前

    我有一个 VGroup 在我的应用程序中,它包含几种类型的自定义组件 MyComponent . 现在我有了一个删除方法 肌力成分 应该运行一次,从 V群 . 我知道我应该用 parentDocument.vgroupId.removeElement() 但是作为一个参考应该传递什么呢?

    注意:我想在方法内从 肌力成分

    更新 :这是我的资料来源: 在我的主应用程序中

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

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

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

    肌力成分 我打电话

    parentDocument.vgroupId.removeElement(this) // get this error => TypeError: Error #1034: Type Coercion failed: cannot convert global@5ed30d1 to mx.core.IVisualElement.
    

    如果我把它当作 this as IVisualElement 我得到一个等于 null

    2 回复  |  直到 12 年前
        1
  •  1
  •   ocodo    14 年前

    这个例子将展示如何做到。

    实例XML

    <?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>
    
        2
  •  0
  •   user1749053    12 年前
    <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>
    
    推荐文章