0

我有一个应用程序宽度的mx:ViewStack组件,每个组件下都有不同的视图组件s:NavigatorContent,如下所示。

<mx:ViewStack id="vsOne" resizeToContent="true">
    <s:NavigatorContent label="First">
        <package:MyFirstComponent id="myFirstComponent"/>
    </s:NavigatorContent>
    <s:NavigatorContent label="Second">
        <package:MySecondComponent id="mySecondComponent"/>
    </s:NavigatorContent>
</mx:ViewStack>

这是package.MyFirstComponent重要的部分..

<s:Button label="Next" click="somethingToGoForward()"/>

我试过的:

  1. 调用somethingToGoForward()视图组件并尝试访问parent vsOne:不工作。
  2. 调用parent.somethingToGoForward()(当此方法与 相同的 mxml 时ViewStack):不工作

如何从包含它的 mxml 文件之外的任何地方更改我ViewStack的 's ?selectedIndex

谢谢。

4

1 回答 1

0

这是一个使用简单事件的示例:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"     initialize="application1_initializeHandler(event)"
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"  xmlns:local="*">
    <fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;

        protected function application1_initializeHandler(event:FlexEvent):void
        {
            viewStack.selectedChild=one;

        }

        protected function one_changeViewHandler(event:FlexEvent):void
        {
            viewStack.selectedChild=two;
        }

        protected function two_changeViewHandler(event:FlexEvent):void
        {
            viewStack.selectedChild=one;

        }

    ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<mx:ViewStack id="viewStack" >
    <local:ccomp id="one" changeView="one_changeViewHandler(event)" text="LAbel ONE">

    </local:ccomp>
    <local:ccomp id="two" changeView="two_changeViewHandler(event)" text="LAbel TWO" >

    </local:ccomp>
</mx:ViewStack>
</s:Application>

ccomp 是一个自定义组件

<?xml version="1.0" encoding="utf-8"?>
    <mx:HBox 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="400" height="300">
<fx:Metadata>
    [Event(name="changeView", type="mx.events.FlexEvent")]  
</fx:Metadata>
<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;

        private var _text:String;

        public function get text():String
        {
            return _text;
        }
        [Bindable]
        public function set text(value:String):void
        {
            _text = value;
        }

        protected function button1_clickHandler(event:MouseEvent):void
        {
            this.dispatchEvent(new FlexEvent("changeView",true));               
        }
    ]]>
</fx:Script>

<fx:Declarations>
</fx:Declarations>
    <s:Button label="Change" click="button1_clickHandler(event)">
    </s:Button>
    <s:Label fontSize="24" text="{text}">
    </s:Label>
   </mx:HBox>

戴维德

于 2013-03-01T14:47:54.023 回答