You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复Xamarin Mac中Array Controller的键值编码兼容问题?

Xamarin Mac Array Controller NSUnknownKeyException 解决方法

问题原因

报错提示<NSApplication 0x7fd1bef04810>不支持键employees的键值编码,核心原因是XIB的File's Owner被默认设置为NSApplication,而employees属性定义在RMDocument类中,绑定目标错误导致找不到对应属性。

具体解决步骤

  • 修正XIB的File's Owner类型
    打开对应的xib文件,在Interface Builder的「Identity Inspector」面板中,将File's Owner的Class设置为你的RMDocument类,替换默认的NSApplication。

  • 检查Array Controller绑定配置
    选中Array Controller,进入「Bindings Inspector」:

    • 确保Content Array的绑定目标为File's Owner(此时已改为RMDocument),Model Key Path保持employees不变
    • 确认Array Controller的Class属性已设置为Person,该配置无需修改
  • 验证RMDocument的属性实现
    确保employees属性符合Key-Value Coding(KVC)要求:

    • 类上需添加[Register("RMDocument")]标记
    • 属性需用[Export]标记暴露给OC runtime,示例代码:
      [Register("RMDocument")]
      public class RMDocument : NSDocument
      {
          private NSMutableArray _employees;
      
          [Export("employees")]
          public NSMutableArray Employees
          {
              get => _employees ??= new NSMutableArray();
              set => _employees = value;
          }
      }
      
  • 确认XIB与RMDocument的关联
    如果是文档型应用,确保RMDocument的构造函数正确加载目标xib:

    public RMDocument() : base("YourXibFileName", NSBundle.MainBundle)
    {
    }
    

内容的提问来源于stack exchange,提问作者OppsCoder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 11:15:33