Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache flex 将事件添加到菜单选项_Apache Flex_Actionscript - Fatal编程技术网

Apache flex 将事件添加到菜单选项

Apache flex 将事件添加到菜单选项,apache-flex,actionscript,Apache Flex,Actionscript,//动作脚本代码 导入mx.controls.Menu; 导入flash.events.MouseEvent private var menu12:Menu; private function init():void { menu12 = new Menu(); menu12.labelField = "@label"; menu12.dataProvider = xmlDP;

//动作脚本代码

导入mx.controls.Menu; 导入flash.events.MouseEvent

        private var menu12:Menu;

        private function init():void {
            menu12 = new Menu();
            menu12.labelField = "@label";
            menu12.dataProvider = xmlDP;
            menu12.showRoot = false;
            menu12.width = popUpButton.width;
            popUpButton.popUp = menu12;
        }
//XML信息

        <menu1 label="Some introduction" />
        <menu2 label="Disabled State (disabled)." enabled="false" />
        <sep1 type="separator" />
        <menu3 label="parent">
            <menu4 label="child1" />
        </menu3>
        <menu5 label="parent (disabled)" enabled="false">
            <menu6 label="child1" />
            <menu7 label="child2" />
            <menu8 label="child3" />
        </menu5>
        <menu9 type="separator" />
        <menu10 type="separator" />
        <menu11 id="leftButton" label="Left" type="radio" groupName="radioGroup" toggled="true" enabled="true"  />
        <menu12 id="rightButton" label="Right" type="radio" groupName="radioGroup" enabled="false" />
        <menu13 id="popupButton" label="Popup" type="radio" groupName="radioGroup" enabled="false" />

    </root>


//组成部分

<mx:PopUpButton id="popUpButton"
            label="Please select an item"
            openAlways="true"
            creationComplete="init();" />

如何向弹出按钮中的菜单添加附加项

添加

menu12.addEventListener(MenuEvent.ITEM\u单击,itemClickHandler)

在init函数的末尾,添加listener函数

私有函数itemClickHandler(事件:MenuEvent):void{}

添加到脚本中。

添加

menu12.addEventListener(MenuEvent.ITEM\u单击,itemClickHandler)

在init函数的末尾,添加listener函数

私有函数itemClickHandler(事件:MenuEvent):void{}


我不认为您可以按照自己的方式在特定的XML节点上放置事件处理程序。您必须查看即将到来的事件,才能判断应该运行哪个处理程序。看看Theo Hultberg的guards概念,它让过滤代码更干净:


我认为您不能按照自己的方式将事件处理程序放在特定的XML节点上。您必须查看即将到来的事件,才能判断应该运行哪个处理程序。看看Theo Hultberg的guards概念,它让过滤代码更干净:

我是这样做的

为每个XML项添加一个属性,以存储每个菜单项的事件名称:

<menu4 label="child1" eventName="child1Event" />
(您甚至可以解析XML以动态添加事件侦听器

private function init():void
{
    menu12 = new Menu();
    ...

    addEventListener("child1Event", 
        function(event:Event)
        {
            alert(event.type);
        }
        );
}
添加itemClick事件处理程序:

private function popUpButton_itemClickHandler(event:MenuEvent):void
{
    if (event.item.@eventName != null)
        dispatchEvent(new Event(event.item.@eventName));
}

<mx:PopUpButton id="popUpButton"
        label="Please select an item"
        openAlways="true"
        creationComplete="init();" 
        itemClick="popUpButton_itemClickHandler(event);" />
private函数PopubButton\u itemClickHandler(事件:menueEvent):void
{
if(event.item.@eventName!=null)
dispatchEvent(新事件(Event.item@eventName));
}
我是这样做的

为每个XML项添加一个属性,以存储每个菜单项的事件名称:

<menu4 label="child1" eventName="child1Event" />
(您甚至可以解析XML以动态添加事件侦听器

private function init():void
{
    menu12 = new Menu();
    ...

    addEventListener("child1Event", 
        function(event:Event)
        {
            alert(event.type);
        }
        );
}
添加itemClick事件处理程序:

private function popUpButton_itemClickHandler(event:MenuEvent):void
{
    if (event.item.@eventName != null)
        dispatchEvent(new Event(event.item.@eventName));
}

<mx:PopUpButton id="popUpButton"
        label="Please select an item"
        openAlways="true"
        creationComplete="init();" 
        itemClick="popUpButton_itemClickHandler(event);" />
private函数PopubButton\u itemClickHandler(事件:menueEvent):void
{
if(event.item.@eventName!=null)
dispatchEvent(新事件(Event.item@eventName));
}
menu12.addEventListener(MenuEvent.ITEM\u CLICK,itemClickHandler);
-这是菜单,但我想添加到每个XML的节点。例如,如果我想在XML中为特定节点添加事件…我该如何做。
menu12.addEventListener(MenuEvent.ITEM\u CLICK,itemClickHandler);
-这是用于菜单的,但我想添加到每个XML的节点。例如,如果我想为XML中的特定节点添加事件…我该如何做。