在Adobe Flex中动态创建菜单?

时间:2012-09-30 20:25:57

标签: actionscript-3 flex flex4 flex3 flash-builder

我有一个Flex应用程序,需要以编程方式生成菜单,它将非常复杂和动态。这样做的最佳技巧是什么?

更新:我已经使用RIAstar建议的菜单项和子属性对象测试了下面的代码。这是有效的,除了我没有看到“输入”菜单,它似乎被旁路。我看到的是:

Screenshot

我期待“添加 - >输入 - >设备{0..8}”。

感谢任何想法,Fred。

<?xml version="1.0" encoding="utf-8"?>

<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.controls.Menu;
        protected function button1_clickHandler(event:MouseEvent):void
        {
            var toplevelMenu:Object = new Object();
            toplevelMenu.label = "Top Level";
            var addMenu:Object = new Object();
            addMenu.label = "Add";
            toplevelMenu.children = addMenu;

            var inputMenu:Object = new Object();
            inputMenu.label = "Input";

            var inputDevicesMenu:ArrayCollection = new ArrayCollection();
            for (var i:int = 0;i < 10;i++) {
                if ((i % 2) == 0) {
                    var inputDeviceMenu:Object = new Object();
                    inputDeviceMenu.label = "Device " + i;
                    inputDevicesMenu.addItem(inputDeviceMenu);
                }
            }
            if (inputDevicesMenu.length > 0) {
                inputMenu.children = inputDevicesMenu;
            }
            addMenu.children = [inputMenu];

            var menu:Menu = Menu.createMenu(this, toplevelMenu, false);
            menu.show(event.stageX, event.stageY);
        }
    ]]>
</fx:Script>

<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Button x="56" y="28" label="Show Menu" click="button1_clickHandler(event)"/>

1 个答案:

答案 0 :(得分:2)

根据RIAstar的建议和你的代码,我在button1_clickHandler函数中做了一些修改: -

更新了函数button1_clickHandler中的代码: -

protected function button1_clickHandler(event:MouseEvent):void
            {
                var addMenu:Object = new Object();
                addMenu.label = "Add";

                var inputMenu:Object = new Object();
                inputMenu.label = "Input";

                var outputMenu:Object = new Object();
                outputMenu.label = "Output";

                var inputOutputDevicesMenu:ArrayCollection = new ArrayCollection();
                inputOutputDevicesMenu.addItem(outputMenu);
                inputOutputDevicesMenu.addItem(inputMenu);

                addMenu.children = inputOutputDevicesMenu;

                var inputDevicesMenu:ArrayCollection = new ArrayCollection();
                for (var i:int = 0;i < 10;i++) {
                    if ((i % 2) == 0) {
                        var inputDeviceMenu:Object = new Object();
                        inputDeviceMenu.label = "Device " + i;
                        inputDevicesMenu.addItem(inputDeviceMenu);
                    }
                }
                if (inputDevicesMenu.length > 0) {
                    inputMenu.children = inputDevicesMenu;
                }

                var menu:Menu = Menu.createMenu(this, addMenu, true);
                menu.show(event.stageX, event.stageY);
            }

希望这可能有助于获得一些想法......