c# - 在 MonoMac 中以编程方式创建 NSMenuItems

标签 c# macos mono nsmenuitem monomac

我正在尝试以编程方式向我的 MonoMac 应用程序添加菜单。我打开了 MainMenu.xib 并从 MainMenu 控件中删除了所有 NSMenuItem

我将以下代码添加到我的 FinishedLaunching 覆盖中:

var fileMenuItem = new NSMenuItem("File");
var fileMenu = new NSMenu();

var fileNew = new NSMenuItem("New");
var fileOpen = new NSMenuItem("Open");
var fileSave = new NSMenuItem("Save");

fileMenu.AddItem(fileNew);
fileMenu.AddItem(fileOpen);
fileMenu.AddItem(fileSave);

fileMenuItem.Menu = fileMenu;

NSApplication.SharedApplication.MainMenu.AddItem(fileMenuItem); 

但它什么也没做。

当我将代码添加到 MainWindowController.Initialize() 时,我得到一个断言失败“要插入菜单的项目已经在另一个菜单中”

我正在移植在此 SO 答案中找到的代码:Creating NSMenu with NSMenuItems in it, programmatically?

最佳答案

原来我必须做以下事情:

fileMenuItem.Submenu = fileMenu;

NSMenuItem 的 Submenu 属性应该设置为实际菜单而不是 Menu 属性。

关于c# - 在 MonoMac 中以编程方式创建 NSMenuItems,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7922911/

相关文章:

c# - 需要一些帮助来破译正则表达式

c# - 如何递归加载Azure blob文件列表?

xcode - 我该如何解决 "NSAlert is being used from a background thread, which is not safe."

.net - .NET向Windows/Linux进程和线程添加了什么?

c# - MonoGame 最大化窗口事件

c# - 方法 'ToString"没有重载在转换日期时采用 1 个参数

json - 如何覆盖 SublimeLinter-CSSLint 中的 "Selectors should not contain IDs"规则?

python - Mac OSX 上 WxPython TaskBarIcon 的鼠标事件未触发

c# - Mono 网络服务和基本身份验证

c# - 单元测试在 Web 应用程序使用的类库中使用配置管理器的方法