处理Xamarin.ios中的部分CloudKit错误 背景

处理Xamarin.ios中的部分CloudKit错误 背景,xamarin,error-handling,xamarin.ios,cloudkit,ckerror,Xamarin,Error Handling,Xamarin.ios,Cloudkit,Ckerror,我将遵循本教程: 他有一个部分来处理用swift编写的CloudKit中的部分错误: public func isSpecificErrorCode(code: CKError.Code) -> Bool { var match = false if self.code == code { match = true } else if self.code == .partialFailure { // This is a mu

我将遵循本教程:

他有一个部分来处理用swift编写的CloudKit中的部分错误:

public func isSpecificErrorCode(code: CKError.Code) -> Bool {
    var match = false
    if self.code == code {
        match = true
    }
    else if self.code == .partialFailure {
        // This is a multiple-issue error. Check the underlying array
        // of errors to see if it contains a match for the error in question.
        guard let errors = partialErrorsByItemID else {
            return false
        }
        for (_, error) in errors {
            if let cke = error as? CKError {
                if cke.code == code {
                    match = true
                    break
                }
            }
        }
    }
    return match
}
我正试图把它翻译成C#来代表Xamarin。但我正在努力处理CloudKit错误,因为Xamarin似乎缺少CKError类。我在一个静态类中编写了以下内容:

    static public bool isSpecificErrorCode(CKErrorCode code, NSError error) {
        var match = false;
        if ((int)code == error.Code) {
            match = true;
        }
        else if (code == CKErrorCode.PartialFailure) {
            // This is a multiple-issue error. Check the underlying array
            // of errors to see if it contains a match for the error in question.
            NSDictionary errors = (NSDictionary)error.UserInfo.ObjectForKey(CKErrorFields.PartialErrorsByItemIdKey);
            foreach (KeyValuePair<NSObject, NSObject> item in errors) {
                var err = (NSError) item.Value;
                if (err.Code == (int)code) {
                    match = true;
                    break;
                }
            }
        }

        return match;
    }
尝试的解决方案(编译错误、缺少数据) 以下是我无力的尝试:

    public class MergeRecordPair {
        public CKRecord clientRecord = null;
        public CKRecord serverRecord = null;
    }

    static public MergeRecordPair getMergeRecords(this NSError error) {
        if (error.Code == (int)CKErrorCode.ServerRecordChanged) {
            // This is the direct case of a simple serverRecordChanged Error.
            
            // here
            return new MergeRecordPair {
                clientRecord = clientRecord, // <-- How do I get these?
                serverRecord = serverRecord // <-- How do I get these?
            };
            // here

        }
        if (error.Code == (int)CKErrorCode.PartialFailure) {
            return new MergeRecordPair();
        }
        
        NSDictionary errors = (NSDictionary)error.UserInfo.ObjectForKey(CKErrorFields.PartialErrorsByItemIdKey);
        if (errors == null) {
            return new MergeRecordPair();
        }
    
        foreach (KeyValuePair<NSObject, NSObject> item in errors) {
            var err = (NSError) item.Value;
            if (err.Code == (int)CKErrorCode.ServerRecordChanged) {
                return err.getMergeRecords();
            }
        }
        return new MergeRecordPair();
    }
公共类合并记录对{
public CKRecord clientRecord=null;
public CKRecord serverRecord=null;
}
静态公共MergeRecordPair getMergeRecords(此N错误){
if(error.Code==(int)CKErrorCode.ServerRecordChanged){
//这是简单serverRecordChanged错误的直接例子。
//这里
返回新的合并记录对{
clientRecord=clientRecord//
    public class MergeRecordPair {
        public CKRecord clientRecord = null;
        public CKRecord serverRecord = null;
    }

    static public MergeRecordPair getMergeRecords(this NSError error) {
        if (error.Code == (int)CKErrorCode.ServerRecordChanged) {
            // This is the direct case of a simple serverRecordChanged Error.
            
            // here
            return new MergeRecordPair {
                clientRecord = clientRecord, // <-- How do I get these?
                serverRecord = serverRecord // <-- How do I get these?
            };
            // here

        }
        if (error.Code == (int)CKErrorCode.PartialFailure) {
            return new MergeRecordPair();
        }
        
        NSDictionary errors = (NSDictionary)error.UserInfo.ObjectForKey(CKErrorFields.PartialErrorsByItemIdKey);
        if (errors == null) {
            return new MergeRecordPair();
        }
    
        foreach (KeyValuePair<NSObject, NSObject> item in errors) {
            var err = (NSError) item.Value;
            if (err.Code == (int)CKErrorCode.ServerRecordChanged) {
                return err.getMergeRecords();
            }
        }
        return new MergeRecordPair();
    }