如何修复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
相关产品推荐
相关产品推荐

