从Flex 4中的ViewStack中删除子内容

时间:2010-09-09 15:37:01

标签: flex actionscript-3 actionscript flex4 mxml

在下面的示例中,当我单击“添加内容”时,新堆栈内容将按预期加载到ViewStack中。但是当我点击“关闭内容”时,我希望它关闭ViewStack中新创建的内容并切换到“defaultContent”内容。

谁能告诉我哪里出错了?提前谢谢。

// TestProject.mxml (application)
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:layout>
        <s:VerticalLayout/>
    </s:layout>
    <fx:Script>
        <![CDATA[
            import com.NewContent;

            private function addContent():void
            {
                var content:NewContent = new NewContent();
                var navContent:NavigatorContent = new NavigatorContent();
                navContent.id = 'newContent';
                navContent.label = 'newContent';
                navContent.width = Number('100%');
                navContent.height = Number('100%');
                navContent.addElement(content);

                viewStack.addElement(navContent);
                viewStack.selectedChild = navContent;
            }
        ]]>
    </fx:Script>
    <mx:ViewStack id="viewStack" width="100%" height="100%">
        <s:NavigatorContent id="defaultContent"
                            label="defaultContent">
            <s:Button click="addContent()" label="Add Content"/>
        </s:NavigatorContent>
    </mx:ViewStack>
</s:WindowedApplication>

// NewContent.mxml (component)
<?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="100%">
    <fx:Script>
        <![CDATA[
            import mx.core.FlexGlobals;
            private function closeContent():void
            {
                FlexGlobals.topLevelApplication.viewStack.removeChild('newContent');
                FlexGlobals.topLevelApplication.viewStack.selectedChild = 'defaultContent';
            }
        ]]>
    </fx:Script>
    <s:Button click="closeContent()" label="Close Content"/>
</s:Group>

2 个答案:

答案 0 :(得分:0)

selectedChild期待孩子本身,而不是标签。

相反 - 试试这个:

public function removeContent():void
{
     Viewstack(this.parent).selectedIndex = 0;
     this.parent.removeChild(this);
}

注意 - 通常建议避免使用FlexGlobals.topLevelApplication,因为它会导致非常紧密耦合的&amp;脆弱的应用。

答案 1 :(得分:0)

对它进行排序......

// TestProject.mxml (application)
private function addContent():void
{
    var content:NewContent = new NewContent();
    content.addEventListener("removeMe",onRemove,false,0,true);
    var navContent:NavigatorContent = new NavigatorContent();
    navContent.id = 'newContent';
    navContent.label = 'newContent';
    navContent.width = Number('100%');
    navContent.height = Number('100%');
    navContent.addElement(content);

    viewStack.addElement(navContent);
    viewStack.selectedChild = navContent;

private function onRemove(event:Event):void
{
    var content:NewContent = event.currentTarget as NewContent;
    content.removeEventListener("removeMe",onRemove,false);
    viewStack.removeChild(content.parent.parent.parent);
}

// NewContent.mxml (component)
public function removeContent():void
{
    dispatchEvent(new Event("removeMe"));
}