Flex:如何在扩展WindowedApplication类的AS3类中创建本机菜单

时间:2017-02-28 14:26:16

标签: actionscript-3 flex menu

我们都知道如何在MXML中使用XML定义本机菜单。它看起来像这样:

<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                   xmlns:s="library://ns.adobe.com/flex/spark"
                   xmlns:mx="library://ns.adobe.com/flex/mx">

<fx:Declarations>
    <!-- The xml data provider for menu -->
    <fx:XML format="e4x" id="menuData">
        <root>
            <menuitem label="File">
                <menuitem label="Open"/>
                <menuitem label="Save"/>
            </menuitem>
            <menuitem label="Help"/>
        </root>
    </fx:XML>
</fx:Declarations>
<s:menu>
    <mx:FlexNativeMenu dataProvider="{menuData}" labelField="@label" showRoot="false"/>
</s:menu>   

但是我需要在扩展WindowedApplication类并由主MXML扩展的类中使用加载的XML创建本机菜单(应用程序菜单)。我怎样才能做到这一点?感谢您提前提供任何帮助!

1 个答案:

答案 0 :(得分:0)

以下是菜单的actionscript类的示例。

package com {
import mx.controls.FlexNativeMenu;
import mx.controls.Menu;

import spark.components.WindowedApplication;

public class MyNativeMenu extends WindowedApplication{

    public var menuData:XML;
    public var flexNativeMenu:FlexNativeMenu;
    public var myMenu:Menu;
    public function MyNativeMenu() {
        menuData = <root>
            <menuitem label="File">
                <menuitem label="Open"/>
                <menuitem label="Save"/>
            </menuitem>
            <menuitem label="Help"/>
        </root>;
        flexNativeMenu = new FlexNativeMenu();
        flexNativeMenu.dataProvider = menuData;
        flexNativeMenu.labelField = "@label";
        flexNativeMenu.showRoot = false;
    }

    override protected function createChildren():void {
        this.menu = flexNativeMenu;
    }
}
}

主要的mxml如下所示

<?xml version="1.0"?>
<com:MyNativeMenu xmlns:fx="http://ns.adobe.com/mxml/2009"     xmlns:s="library://ns.adobe.com/flex/spark"
              xmlns:com="com.*">
<fx:Script><![CDATA[
    ]]></fx:Script>
</com:MyNativeMenu>