2012-12-17 145 views
0

這是主要的應用使用自定義事件

<?xml version="1.0" encoding="utf-8"?> 
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
      verticalAlign="middle" 
      backgroundColor="white" layout="absolute" initialize="init();" xmlns:MyComp="screens.*" > 
      <mx:Script> 
       <![CDATA[ 
       import screens.MyEvent; 

        private function init():void 
        { 
         systemManager.addEventListener("data_transfer",handleDataTransfer); 

        } 
        private function handleDataTransfer(evt:MyEvent):void{ 

       this.myViewStack.selectedIndex=1; 

       } 


       ]]> 
      </mx:Script> 


    <mx:ViewStack id="myViewStack" selectedIndex="0" width="1110" height="636"> 
    <MyComp:Welcome /> 
     <MyComp:Screen id="fillPage" /> 
     </mx:ViewStack> 
    </mx:Application> 

這是我歡迎分量

<?xml version="1.0" encoding="utf-8"?> 
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="930" height="300" > 
<mx:Script> 
    <![CDATA[ 
     private function changeHandler():void{ 
     var myEVT:MyEvent = new MyEvent("data_transfer",false, true); 
     this.dispatchEvent(myEVT); 
     } 

    ]]> 
</mx:Script> 
<mx:Button label="Fill The Form" id="fillForm" click="changeHandler()"/>   
</mx:Canvas> 

從子組件改變變量的狀態,這是我的screen.mxml

<?xml version="1.0" encoding="utf-8"?> 
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300"> 
<mx:Label text="hai"> 
</mx:Label> 
</mx:Canvas> 

這是MYEvent.as

package screens 
{ 
    import flash.events.Event; 

    public class MyEvent extends Event 
    { 

    public static const DATA_TRANSFER:String = "data_transfer"; 

     public function MyEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false) 
     { 
      super(type, bubbles, cancelable); 
     } 

    } 
} 

我想通過單擊歡迎MXML按鈕來獲取屏幕MXML ...我最初學習.. 但它不是working..Please幫助我..

回答

0

當您的自定義事件有bubbling = false,它不會冒泡到systemManager。嘗試:

var myEVT:MyEvent = new MyEvent("data_transfer", true, true); 

另外,監聽器添加到正在調度此事件的對象:

welcomePage.addEventListener("data_transfer",handleDataTransfer); 
.... 
<MyComp:Welcome id="welcomePage" /> 

第二類解決方案通常更好/首選是 - 少事件冒泡是一件好事

+0

才能在沒有使用事件的情況下執行此任務...在這種情況下,當單擊按鈕時(id = fillForm),我們可以在不使用自定義事件的情況下編寫click =「this.parent.myviewstack.currentIndex = 1」。 – user533

+0

yes you可以 - 但事件的好處是他們更靈活。如果您決定重新排列組件的佈局,則必須手動調整所有路徑(this.parent.myComponent.etc)。 – 2012-12-21 04:44:20