我在Flex的主應用程序中有一個菜單欄和一個自定義組件。在單擊菜單欄中的菜單項時,我需要一個可以被自定義組件監聽的自定義事件。或者它也可以是菜單事件,也可以在選擇其中一個菜單項時引發。我怎樣才能做到這一點?Flex中兄弟對象之間的事件流
如果我沒有錯,菜單項點擊事件將從菜單欄傳播到應用程序(在冒泡階段),並且不會去自定義組件,這是它的兄弟姐妹。
例如代碼:
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:code="http://code.google.com/p/flexlib/" >
<fx:Script>
<![CDATA[
protected function menuBar_itemClickHandler(event:MenuEvent):void
{
if([email protected]=='New File'){
//I dont want to write code like
// mainTab.someProperty= someproperty
//Instead if this event or other custom event
//be raised which could be listened by the custom
//component and the listener could be written in the
//component itself
}
}
]]>
</fx:Script>
<mx:MenuBar id="menuBar"
labelField="@label"
showRoot="false"
width="100%" height="4%"
horizontalCenter="0" verticalCenter="0"
itemClick="menuBar_itemClickHandler(event)">
<mx:dataProvider>
<fx:XML>
<root>
<parent label="File">
<node label="New File" />
<node label="Load" />
<node label="Save" />
<node label="Exit" />
</parent>
</root>
</fx:XML>
</mx:dataProvider>
</mx:MenuBar>
<code:MDICanvas width="100%" height="95%">
<!-- MY CUSTOM COMPONENT -->
<local:MainTab id="mainTab" width="100%" height="100%" />
</code:MDICanvas>
</s:Application>
我認爲這是實現它的正確方法。 –
如果它們是兄弟姐妹並存在於相同的範圍內,您能獲得MenuBar和addEventListener()的引用嗎? –
事件只能向上/向下傳遞父子層次結構。它不能去兄弟姐妹。有兩種解決方法 - 1)保持其他組件中的組件和addEventListener中的組件的引用。 2)兩個父組件中的addEventListener(這裏是Application)。 – Asad