2008-10-15 33 views
2

這與我以前的發佈類似。但是這次我想調用主mxml頁面上存在的函數。Flex:包含組件的調用函數

這是我的主MXML頁:

main.mxml

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:ns1="*"> 
    <mx:Script> 
     <![CDATA[ 
     public function changeText(currentText:String):void{ 

      switch (currentText){ 
       case "changeText": 
        lblOne.text = "More Text";     
      } 
     } 
      ]]> 
    </mx:Script> 

    <mx:HBox x="137.5" y="10" width="100%" height="100%"> 
     <ns1:menu id="buttons"> </ns1:menu> 
    </mx:HBox> 
    <mx:Canvas x="137" y="88" width="408.5" height="200"> 
     <mx:HBox x="0" y="10" width="388.5" height="190"> 
      <mx:Panel width="388" height="179" layout="absolute"> 
       <mx:Label x="10" y="10" text="Some Text" visible="{buttons.showLabel}" id="lblOne"/> 
      </mx:Panel> 
     </mx:HBox> 
    </mx:Canvas> 
</mx:Application> 

這裏是我的網頁包括:

menu.mxml

<?xml version="1.0" encoding="utf-8"?> 
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300"> 
    <mx:Script> 
     <![CDATA[ 
      [Bindable] public var showLabel:Boolean = true; 
     ]]> 
    </mx:Script> 
    <mx:MenuBar width="380" height="58"></mx:MenuBar> 
    <mx:Button x="10" y="10" width="80" label="Show" id="btnOne" click="this.showLabel=true;" /> 
    <mx:Button x="94" y="10" width="80" label="Hide" id="btnTwo" click="this.showLabel=false;"/> 
    <mx:Button x="181" y="10" width="80" label="Run Function" id="btnThree" click="{changeText('changeText')}"/> 
</mx:Canvas> 

如何調用menu.mxml上的按鈕的changeText函數?

回答

3

一下添加到菜單:

<mx:Metadata> 
     [Event(name="buttonClicked", type="flash.events.Event")] 
    </mx:Metadata> 

<mx:Button x="10" y="10" width="80" label="Show" id="btnOne" click="this.showLabel=true;dispatchEvent(new Event("buttonClicked"));"/> 

變化主要以:

<ns1:menu id="buttons" buttonClicked="changeText("Your Text");"> 

我不知道哪裏當前文本是來自,但如果是從菜單中,您可能必須建立自己的自定義彈性事件或爲兩部分訪問創建一個公共變量。第一個通常是首選。

P.S.事件元數據事物也可以通過在應用程序的創建完成時添加事件偵聽器來實現。你會添加到主:

buttons.addEventListener("buttonClicked",changeText("Your Text")); 
0

有一個更簡單的方法,只需使用parentDocument。

更改此:

<mx:Button x="181" y="10" width="80" label="Run Function" id="btnThree" click="{changeText('changeText')}"/> 

到:

<mx:Button x="181" y="10" width="80" label="Run Function" id="btnThree" click="{parentDocument*.changeText('changeText')}"/>** 
相關問題