Home > Blockchain >  How can I fix coding-compliant for the key for Xamarin Mac?
How can I fix coding-compliant for the key for Xamarin Mac?

Time:08-17

Recently I am testing Array Controller from xamarin mac. Here are the NSDocument class I have.

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";
        }
}

Then I have a Person class as follows

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;
    }
}

At last, I have Array Controller in xib, with each pages filled.

"Attributes Inspector" -> Class setted to Person, Keys updated.

"Binding Inspector" -> I choosed "File's Owner" as a target, and Model Key Path setted to "employees".

Then I run the application, but got error messages saying "Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSApplication 0x7fd1bef04810> valueForUndefinedKey:]: this class is not key value coding-compliant for the key employees."

Anyone has any clues how to fix it?

Thank you.

CodePudding user response:

I finally fixed it. Here are solutions.

  1. Person class must be exported as

    [Register ("PersonModel")] - {class}Model
    public class Person{}
    
  2. [Export("employees")] - **wrong export**
    public NSMutableArray employees {}
    

    must be exported as {exportclass}Array

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

OMG, C# REALLY kills me, which took me 1 week to find the issues.

  • Related