0

We all know how to define a native menu using XML in MXML. It looks like this:

<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>   

But I need to make a native menu(application menu) using a loaded XML in a class which extends the WindowedApplication class and is extended by the main MXML. How can I do this? Thanks for any help in advance!

Corona S.
  • 47
  • 6

1 Answers1

0

Below is the example of actionscript class for menu.

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

And the main mxml is like below

<?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>
Sumit
  • 729
  • 4
  • 9
  • Hi Sumit, thanks a lot! This works! But how can I find out, which function should I overwrite? I think you can also answer this question of mine.[link](http://stackoverflow.com/questions/42549177/flex4-how-to-create-the-view-states-in-as-class-code-behind) – Corona S. Mar 02 '17 at 08:02
  • Depends upon what functionality you want to override. Well if you want to add more components to your component class you can override createChildren method. In your case you can actually move the line in createChildren method to constructor method itself, as we are just assigning the flexnativemenu to menu. – Sumit Mar 02 '17 at 10:18
  • Thanks for your explanation :) – Corona S. Mar 02 '17 at 11:15