2

I have a menubar and a custom component in my main application in Flex. On clicking a menu item in the menubar, I need a custom event to be raised which could be listened by the custom component as well. Or it can be the menu event also, raised when selecting one of the menu items. How can i accomplish this?

If I am not wrong the menu item click event will propagate from the menubar to the application (in bubbling phase) and won't go the custom component which is its sibling.

Example Code:

 <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(event.item.@label=='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>
4

1 回答 1

0

使用 parsley 、 swiz 、机器人腿等应用程序框架......轻松完成此任务。您可以在任何范围内侦听和传播事件。

于 2013-07-28T18:14:05.990 回答