2

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

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

截屏

我期待“添加->输入->设备{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)"/>

4

1 回答 1

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

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

于 2012-10-03T05:20:10.993 回答