一旦调用了本地上下文菜单,我需要从一组远程服务调用中填充它。 但是,当前的实现需要在打开菜单之前设置项目。
有什么想法吗?
谢谢;) 甲
答案 0 :(得分:0)
window.htmlLoader.contextMenu = new air.NativeMenu();
menu1 = new air.NativeMenu();
loading=new air.NativeMenuItem("Loading...");
menu1.addItem(loading);
menu1.addEventListener(air.Event.DISPLAYING, addMenu1Items);
function addMenu1Items(){
menu1.removeItem(loading);
item11 = new air.NativeMenuItem("Item 1");
item11.addEventListener(air.Event.SELECT, function(e){alert("You clicked Menu 1,Item 1");e.preventDefault();});
item12 = new air.NativeMenuItem("Item 2");
item12.addEventListener(air.Event.SELECT, function(e){alert("You clicked Menu 1,Item 2");e.preventDefault();});
menu1.addItem(item11);
menu1.addItem(item12);
}
我刚刚使用HTML / Javascript应用程序进行了测试,以下内容似乎对我有效(从http://www.adobe.com/devnet/air/ajax/quickstart/adding_menus.html无耻地翻录和修改,如果您碰巧使用Flex,则有以下版本:那也是:http://www.adobe.com/devnet/air/flex/quickstart/adding_menus.html)。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Context</title>
<script src="lib/air/AIRAliases.js" language="JavaScript" type="text/javascript"></script>
<script type="text/javascript">
window.htmlLoader.contextMenu = new air.NativeMenu();
menu1 = new air.NativeMenu();
item11=new air.NativeMenuItem("Item 1");
item11.addEventListener(air.Event.SELECT,function(e){alert("You clicked Menu 1,Item 1");e.preventDefault();});
item12=new air.NativeMenuItem("Item 2");
item12.addEventListener(air.Event.SELECT,function(e){alert("You clicked Menu 1,Item 2");e.preventDefault();});
menu1.addItem(item11);
menu1.addItem(item12);
menu2 = new air.NativeMenu();
item21=new air.NativeMenuItem("Item 1");
item21.addEventListener(air.Event.SELECT,function(e){alert("You clicked Menu 2,Item 1");e.preventDefault();});
item22=new air.NativeMenuItem("Item 2");
item22.addEventListener(air.Event.SELECT,function(e){alert("You clicked Menu 2,Item 2");e.preventDefault();});
menu2.addItem(item21);
menu2.addItem(item22);
window.htmlLoader.contextMenu.addSubmenu(menu1,"Menu 1");
window.htmlLoader.contextMenu.addSubmenu(menu2,"Menu 2");
//Displays the context menu
function showContextMenu(event){
event.preventDefault();
window.htmlLoader.contextMenu.display(window.nativeWindow.stage, event.clientX, event.clientY);
}
function addItemToContextMenu(){
menux = new air.NativeMenu();
item=new air.NativeMenuItem("Item &1");
item.addEventListener(air.Event.SELECT,function(e){alert("You clicked Menu X,Item 1");e.preventDefault();});
menux.addItem(item);
menux.addItem(new air.NativeMenuItem("Item &2"));
window.htmlLoader.contextMenu.addSubmenu(menux,"Menu X");
}
function clearAndAdd() {
window.htmlLoader.contextMenu = new air.NativeMenu();
addItemToContextMenu();
}
</script>
</head>
<body id="body">
<div id="contextDiv" oncontextmenu="showContextMenu(event)">
Right click to bring up the context menu.<br><br>
<input type="button" value="Add Item To Context Menu" onclick="addItemToContextMenu()"><input type="button" value="Clear Menu and Add Item to Context Menu" onclick="clearAndAdd();">
</div>
</body>
</html>