gpt4 book ai didi

c# - Xamarin:绑定(bind) ios 协议(protocol)/委托(delegate)无法访问 structs.cs 中定义的枚举

转载 作者:可可西里 更新时间:2023-11-01 04:49:53 24 4
gpt4 key购买 nike

我目前正在为 EDQueue library. 创建 ios 绑定(bind)

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
}

}

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

如所写,会产生以下错误:错误 CS0426:文件 EDQueueDelegate.g.cs 中的类型“EDQueue”(CS0426) (EDQueue) 中不存在类型名称“EDQueueResult”

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

//
// 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...
}

但是,如果我删除或注释掉 [Protocol, Model] 位,库构建不会出错。

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

Structs.cs 文件的构建操作设置为 ObjcBindingCoreSource

非常感谢任何帮助。谢谢!

最佳答案

由于您同时拥有命名空间 EDQueue 和名为 EDQueue 的类型(接口(interface)) - 您不能使用 引用命名空间 EDQueue 中的任何类型EDQueue.TypeName,所以这条语句:

ReturnType = typeof (EDQueue.EDQueueResult)

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

解决此问题的最明显方法是不要让命名空间和类型同名,因此重命名命名空间或类型名称。

如果那不可能或者您不完全确定它不会有副作用 - 将引用更改为

ReturnType = typeof (EDQueueResult)

并将 using EDQueue 添加到 usings block 。或者,像这样引用:

ReturnType = typeof (global::EDQueue.EDQueueResult)

关于c# - Xamarin:绑定(bind) ios 协议(protocol)/委托(delegate)无法访问 structs.cs 中定义的枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43966756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com