【问题标题】:programmatically created NSMenu is only showing the first item以编程方式创建的 NSMenu 仅显示第一项
【发布时间】:2016-06-29 16:13:26
【问题描述】:

我正在编写一个使用最少 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,我会看到该菜单,其名称再次更改为我的应用程序名称,并且视图菜单根本不会显示。

我不介意更改标题;我想Apple希望始终显示活动应用程序的名称。但让我感到困扰的是,我无法显示其他菜单。

如何让我的所有菜单显示在屏幕顶部?

【问题讨论】:

  • fileMenuappMenu 缺少标题。

标签: cocoa xamarin appkit nsmenu nsmenuitem


【解决方案1】:

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

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

第一个的标题还是改成应用的名字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 2014-03-02
    • 2019-04-30
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多