如何修复Xamarin Mac密钥的编码兼容性?

dw1jzc5e  于 2022-12-07  发布在  Mac
关注(0)|答案(1)|浏览(134)

最近我在测试xamarin mac的数组控制器。下面是我的NSDocument类。

public partial class RMDocument : AppKit.NSDocument
{
    private NSMutableArray _employees = new NSMutableArray();

    [Export("employees")]
    public NSMutableArray employees {
        get {
            return _employees;
        }
        set { 
            if (_employees == value)
                return;
            _employees = value;
        }
    }

    // Called when created from unmanaged code
    public RMDocument(IntPtr handle) : base(handle)
    {
    }

    // Called when created directly from a XIB file
    //[Export("initWithCoder:")]
    //public RMDocument(NSCoder coder) : base(coder)
    //{
    //}

    public override void WindowControllerDidLoadNib(NSWindowController windowController)
    {
        base.WindowControllerDidLoadNib(windowController);

        // Add code to here after the controller has loaded the document window
    }

    // 
    // Save support:
    //    Override one of GetAsData, GetAsFileWrapper, or WriteToUrl.
    //

    // This method should store the contents of the document using the given typeName
    // on the return NSData value.
    public override NSData GetAsData(string documentType, out NSError outError)
    {
        outError = NSError.FromDomain(NSError.OsStatusErrorDomain, -4);
        return null;
    }

    // 
    // Load support:
    //    Override one of ReadFromData, ReadFromFileWrapper or ReadFromUrl
    //
    public override bool ReadFromData(NSData data, string typeName, out NSError outError)
    {
        outError = NSError.FromDomain(NSError.OsStatusErrorDomain, -4);
        return false;
    }

    // If this returns the name of a NIB file instead of null, a NSDocumentController 
    // is automatically created for you.
    public override string WindowNibName
    {
        get
        {
            return "RMDocument";
        }
}

然后我有一个Person类,如下所示

public partial class Person : NSObject
{
    private NSString _personalName;
    [Export("personalName")]
    public NSString personalName {
        get {
            return _personalName;
        }
        set {
            _personalName = value;
        }
    }

    private float _expectedRaise;
    [Export("expectedRaise")]
    public float expectedRaise {
        get {
            return _expectedRaise;
        }
        set {
            _expectedRaise = value;
        }
    }

    public Person (IntPtr handle) : base (handle)
    {
    }

    public Person() {
        personalName = new NSString("New Person");
        expectedRaise = 0.05f;
    }
}

最后,我在xib中创建了Array Controller,每个页面都被填充。
“属性检查器”-〉类设置为人员,键更新。
“Binding Inspector”-〉我选择了“File 's Owner”作为目标,并将模型键路径设置为“employees”。
然后我运行应用程序,但收到错误消息"Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSApplication 0x7fd1bef04810> valueForUndefinedKey:]: this class is not key value coding-compliant for the key employees."
有人知道怎么修吗?

  • 谢谢-谢谢
zi8p0yeb

zi8p0yeb1#

我终于修好了。下面是解决办法。

  1. Person类必须导出为
[Register ("PersonModel")] - {class}Model
public class Person{}
[Export("employees")] - **wrong export**
public NSMutableArray employees {}

必须导出为{exportclass}数组

[Export("personModelArray")] - {exportclass}Array
 public NSMutableArray employees
 {}

天哪,C#真的让我很难受,我花了一个星期才找到问题所在。

相关问题