actionscript-3 - 在 Adob​​e Flex 中动态创建菜单?

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

我有一个 Flex 应用程序,需要以编程方式生成菜单,它将非常复杂和动态。执行此操作的最佳技术是什么?

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

Screenshot

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

感谢您的任何想法,弗雷德。

<?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)"/>

最佳答案

根据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);
            }

希望这可以帮助您获得一些想法......

关于actionscript-3 - 在 Adob​​e Flex 中动态创建菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12664840/

相关文章:

flash - actionscript3 中嵌入的 mp3 不会一直播放

actionscript-3 - Flex编译器错误1120

ios - 卸载应用程序时,有没有不删除文件目录的方法?

android - 适用于 Android 的 Flex 多页表单

apache-flex - 我们可以从多个 Flex 应用程序访问单个 SQLite 数据库吗?

javascript - FlashExternalInterface和函数回调问题

apache-flex - 在完全加载之前停止加载 Flex 模块的正确方法是什么?

apache-flex - 服务对象返回

apache-flex - 在 Flex 中如何刷新数据网格上的数据提供者

actionscript-3 - Flex 4 自定义 TextInput 皮肤 -> 新图形元素由于某种原因变暗了吗?