如何在flex中访问自定义组件外的公共函数?

时间:2013-02-28 15:00:12

标签: actionscript-3 flex

我的应用程序宽度为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()并使用triyng访问parent vsOne:不起作用。
  2. 调用parent.somethingToGoForward()(当此方法与ViewStack位于同一mxml中时):不工作
  3. 如何从包含它的mxml文件之外的任何位置更改ViewStack的{​​{1}}?

    感谢。

1 个答案:

答案 0 :(得分: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>

的Davide