我正在编写一个使用最少 IB 的 Mac 应用程序 - 请参阅 programatically create initial window of cocoa app (OS X) .
这是我的代码(通过 Xamarin/C#):
public override void DidFinishLaunching(NSNotification notification) {
// create and show a window, then . . .
NSMenu fileMenu = new NSMenu();
NSMenu appMenu = new NSMenu();
// add some items to both menus, then . . .
NSMenuItem file = new NSMenuItem("file");
file.Submenu = fileMenu;
NSMenuItem app = new NSMenuItem("app");
app.Submenu = appMenu;
NSMenu topLevelMenu = new NSMenu();
topLevelMenu.AddItem(app); // these two lines in
topLevelMenu.AddItem(file); // either order
NSApplication.SharedApplication.Menu = topLevelMenu; // incidentally, setting the MainMenu doesn't appear to do anything
}
}
奇怪的是,只显示我添加的第一个菜单项,并且其标题更改为我的应用程序的名称。因此,对于上面的代码,我只会看到标题为 MyAppName 的应用程序菜单,但项目是正确的。如果我首先添加文件 NSMenuItem,我会看到该菜单,其名称再次更改为我的应用程序名称,并且 View 菜单根本不会显示。
我不介意更改标题;我想苹果希望始终显示事件应用程序的名称。但令我烦恼的是我无法显示附加菜单。
如何让所有菜单显示在屏幕顶部?
最佳答案
我只需要向我的菜单添加标题:
fileMenu.Title = "file";
appMenu.Title = "whatever";
第一个标题仍然更改为应用程序的名称。
关于cocoa - 以编程方式创建的 NSMenu 仅显示第一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38105018/