通过编程创建的NSMenu只显示第一项



我正在编写一个Mac应用程序,使用最少的IB -参见编程创建cocoa应用程序的初始窗口(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,我就会看到那个菜单,它的名字还是改成了我的应用名,而视图菜单根本不会显示。

我不介意改变标题;我想苹果公司希望总是显示活动应用程序的名称,但我无法显示额外的菜单,这让我很恼火。

我怎样才能让我所有的菜单显示在屏幕的顶部?

我只需要给我的菜单添加标题:

fileMenu.Title = "file";
appMenu.Title = "whatever";

第一个的标题仍然更改为应用程序的名称。

最新更新