简体   繁体   English

flex菜单栏单击事件

[英]flex menubar clicking event

I have a menuBar that looks like this: 我有一个menuBar看起来像这样:

<mx:MenuBar id="myMenuBar" labelField="@label" cornerRadius="8" color="black" fillColors="[green, green]" itemClick="menuItemClickHandler(event);"
        dataProvider="{menuBarCollection}" change="onTopSelection(event)" />    

The XML for my menuBar looks like this: 我的menuBar的XML如下所示:

            <menuitem label="Vision">
        </menuitem>
        <menuitem label="About">
            <menuitem label="Our Team"/>
            <menuitem label="Services"/>
        </menuitem>

        <menuitem label="Contact Us">

        </menuitem>

As you can see there is Vision and Contact Us but the eventHandler doesn't know when those two are clicked. 如您所见,这里有Vision和Contact Us,但eventHandler不知道何时单击这两个。 What is the correct way to implement the eventHandler? 实现eventHandler的正确方法是什么?

You have two event handlers in your code. 您的代码中有两个事件处理程序。 onTopSelection and menyItemClickHandler. onTopSelection和menyItemClickHandler。 The change event is dispatched when anything is clicked. 单击任何内容时,将调度change事件。 The itemClick event is only dispatches when a submenu item is clicked. 仅在单击子菜单项时调度itemClick事件。

I took the code you shared with us and turned it into a running sample. 我拿走了您与我们共享的代码,并将其变成了一个正在运行的示例。 IF you run it in debug mode, you will see the traces that illustrate what I stated above: 如果您以调试模式运行它,您将看到说明我上面所说的跟踪:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600"
                 creationComplete="application1_creationCompleteHandler(event)">


    <mx:Script>
        <![CDATA[
            import mx.collections.XMLListCollection;
            import mx.events.FlexEvent;
            import mx.events.MenuEvent;

            [Bindable] 
            public var menuBarCollectionSource : XML = 
            <menu>
            <menuitem label="Vision">
            </menuitem>
            <menuitem label="About">
                <menuitem label="Our Team"/>
                <menuitem label="Services"/>
            </menuitem>
            <menuitem label="Contact Us">
            </menuitem>
            </menu>;

            [Bindable] 
            public var menuBarCollection : XMLListCollection = new XMLListCollection();

            public function menuItemClickHandler(event:MenuEvent):void{
                trace('menu item clicked ' + event.label);
            }

            protected function onTopSelection(event:MenuEvent):void
            {
                trace('change to ' + event.label);
            }


            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                menuBarCollection.source = menuBarCollectionSource.menuitem as XMLList;
            }

        ]]>
    </mx:Script>


    <mx:MenuBar id="myMenuBar" labelField="@label" cornerRadius="8" 
                color="black" fillColors="[green, green]" 
                itemClick="menuItemClickHandler(event);"
                dataProvider="{menuBarCollection}" change="onTopSelection(event)" />  

</mx:Application>

Since you're question was about the proper way to implement an event handler, this sample shows I implemented both handlers you had in your original code. 由于您所质疑的是实现事件处理程序的正确方法,因此此示例显示了我实现了您在原始代码中拥有的两个处理程序。

@SuperString you are not really using MenuBar as it was intended. @SuperString,您实际上并没有按预期使用MenuBar。 As Flextras points out, the itemClick event only fires for sub-menu clicks. 正如Flextras指出的那样,itemClick事件仅针对子菜单点击触发。

Think of it like the menu bar in a typical windows application. 可以将其视为典型Windows应用程序中的菜单栏。 Clicking on the File menu brings up a sub-menu, and then only when you click on an item in the sub-menu does something actually happen. 单击“ 文件”菜单将打开一个子菜单,然后只有在单击该子菜单中的某个项目时,才会发生实际的事情。 I am not saying that what you are trying to do is wrong, just that the control wasn't designed with your particular use in mind. 我并不是说您想做的事情是错误的,只是控件的设计没有考虑到您的特定用途。

One suggestion is to use a ButtonBar with a PopupMenu as needed for sub-selections. 一种建议是根据子选择的需要,将ButtonBar与PopupMenu一起使用。 It won't be as clean as you will have to handle events from the Menu differently from the ButtonBar. 它不会像您必须从菜单上处理与ButtonBar不同的方式处理事件那样干净。

Ideally, what you want is a "Nested Navigation Menu" component, but I don't see any existing component with that functionality. 理想情况下,您想要的是“嵌套导航菜单”组件,但是我看不到具有该功能的任何现有组件。 Maybe something for Flextras to build? 也许Flextras需要一些东西?

Thank you very much for your advise ! 非常感谢您的指教!

With your help, I added some events which will be launched from the child elements of my menuBar, but in a slightly different way: 在您的帮助下,我添加了一些事件,这些事件将从menuBar的子元素启动,但方式略有不同:

First, I added an eventlistener to my menu: 首先,我在菜单中添加了一个事件监听器:

(let's say that I have a mx:Menubar called _myMenuBarName) (假设我有一个名为_myMenuBarName的mx:Menubar)

_myMenuBarName.addEventListener(MenuEvent.ITEM_CLICK, eventsLaucher);

Then I created a function which will decide which event it should launch, depending on the label of the menuBarItem 然后,我创建了一个函数,该函数将决定应该启动哪个事件,具体取决于menuBarItem的标签

    private function eventsLauncher(pEvent:MenuEvent):void 
            {
            switch(pEvent.label)
 {
            case "Our Team":
                         {
                //do something
                break;

                case "Vision":
                    //do something else
                break;              
            }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM