2013-02-28 62 views
0

我有一個應用程序寬度a mx:ViewStack組件,每個s:NavigatorContent下面有不同的視圖組件。如何在flex中訪問自定義組件外的公共函數?

<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訪問到parentvsOne :不要工作。
  2. 調用parent.somethingToGoForward()(當該方法是在同一個MXML爲ViewStack):不要工作

我怎樣才能改變我的ViewStackselectedIndex從包含它的MXML文件以外的任何地方?

謝謝。

+0

這是否幫助:https://www.flextras.com/blog /index.cfm/2013/2/1/How-does-one-Flex-Component-talk-to-another-Flex-Component – JeffryHouser 2013-02-28 15:30:32

+0

活動..當然..謝謝..很好的幫助! – gustyaquino 2013-02-28 16:50:05

回答

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> 

達維德

相關問題