Xamarin:绑定ios协议/委托不能访问在structs.cs中定义的枚举

ast*_*ohn 5 c# binding xamarin.ios ios xamarin

我目前正在为EDQueue库创建ios绑定.

Structs.cs文件看起来像这样:

using System;
using ObjCRuntime;

namespace EDQueue
{


    // => Enums attributed with[NativeAttribute] must have an underlying type of `long` or `ulong`
    [Native]
    public enum EDQueueResult : long
    {
        Success = 0,
        Fail,
        Critical
    }

}
Run Code Online (Sandbox Code Playgroud)

ApiDefinition.cs文件类似于:

using System;
using Foundation;
using ObjCRuntime;

namespace EDQueue
{
    // typedef void (^EDQueueCompletionBlock)(EDQueueResult);
    delegate void EDQueueCompletionBlock(EDQueueResult result);

    // ETC....

    // @protocol EDQueueDelegate <NSObject>
    [Protocol, Model]
    [BaseType(typeof(NSObject))]
    interface EDQueueDelegate
    {
        // @optional -(EDQueueResult)queue:(EDQueue *)queue processJob:(NSDictionary *)job;
        [Export("queue:processJob:")]
        EDQueueResult Queue(EDQueue queue, NSDictionary job);

        //// @optional -(void)queue:(EDQueue *)queue processJob:(NSDictionary *)job completion:(EDQueueCompletionBlock)block;
        //[Export("queue:processJob:completion:")]
        //void Queue(EDQueue queue, NSDictionary job, EDQueueCompletionBlock completeBlock);

    }

    // ETC...
}
Run Code Online (Sandbox Code Playgroud)

如上所述,产生以下错误: 错误CS0426:文件类型'EDQueue'(CS0426)(EDQueue)不存在类型名称'EDQueueResult'EDQueueDelegate.g.cs

抛出错误时,该文件如下所示:

//
// Auto-generated from generator.cs, do not edit
//
// We keep references to objects, so warning 414 is expected

#pragma warning disable 414

using System;
using System.Drawing;
using System.Diagnostics;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using UIKit;
using GLKit;
using Metal;
using MapKit;
using ModelIO;
using SceneKit;
using Security;
using AudioUnit;
using CoreVideo;
using CoreMedia;
using QuickLook;
using Foundation;
using CoreMotion;
using ObjCRuntime;
using AddressBook;
using CoreGraphics;
using CoreLocation;
using AVFoundation;
using NewsstandKit;
using CoreAnimation;
using CoreFoundation;

namespace EDQueue {
    [Protocol (Name = "EDQueueDelegate", WrapperType = typeof (EDQueueDelegateWrapper))]
    [ProtocolMember (IsRequired = false, IsProperty = false, IsStatic = false, Name = "Queue", Selector = "queue:processJob:", ReturnType = typeof (EDQueue.EDQueueResult), ParameterType = new Type [] { typeof (global::EDQueue.EDQueue), typeof (NSDictionary) }, ParameterByRef = new bool [] { false, false })]
    public interface IEDQueueDelegate : INativeObject, IDisposable
    {
    }
// ETC...
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我删除或注释掉该[Protocol, Model]位,则库会构建没有错误.

如果我用第二个函数取消注释,我也会得到类似的错误EDQueueCompletionBlock,最终依赖于EDQueueResult枚举.

Structs.cs文件的生成操作设置为ObjcBindingCoreSource.

真的很感激任何帮助.谢谢!

Evk*_*Evk 2

由于您同时具有命名空间EDQueue和类型(接口)命名EDQueue- 您无法使用 引用命名空间中的任何类型EDQueueEDQueue.TypeName因此以下语句:

ReturnType = typeof (EDQueue.EDQueueResult)
Run Code Online (Sandbox Code Playgroud)

不会编译,因为编译器将搜索EDQueue接口内部的成员(即使接口不能有子类,也不能有静态成员)而不是EDQueue命名空间内的成员。

解决此问题的最明显方法是永远不要使命名空间和类型具有相同的名称,因此请重命名命名空间或类型名称。

如果这是不可能的,或者您不完全确定它不会有副作用 - 将参考更改为

ReturnType = typeof (EDQueueResult)
Run Code Online (Sandbox Code Playgroud)

并添加using EDQueue到 usings 块中。或者,像这样参考:

ReturnType = typeof (global::EDQueue.EDQueueResult)
Run Code Online (Sandbox Code Playgroud)