1

我正在创建一个使用 Socket Mobile 的 SocketScan SDK 的 Xamarin ios 应用程序。创建了一个绑定项目,将静态库转换为 dll。但无法使用 SktClassFactory.CreateScanObject() 方法创建 ISktScanObject 实例。由于 System.MemberAccessException,该实例未完全创建。请看下面的代码

// A 1:1 of ApiHelper's Objective-C open: method
public void Open ()
{
    _deviceInfoList.Clear ();

    if (_noDeviceText != null) {
        _deviceInfoList.Add (_noDeviceText, _noDeviceText);
    }

    if (_scanObjectReceived != null) {
        SktClassFactory.ReleaseScanObject (_scanObjectReceived);
    }
    _scanObjectReceived = SktClassFactory.CreateScanObject (); //Two properties inside _scanObjectReceived are not getting created due to System.MemberAccessException. 

    Task.Run (() => InitializeScanAPIThread ());

    _scanApiOpen = true;
}

以下是我在 _scanObjectReceived = SktClassFactory.CreateScanObject (); 行之后调试 _scanObjectReceived 对象时收到的异常消息;

  1. Msg System.MemberAccessException:无法创建 ScanAPI.ISktScanMsg 的实例,因为它是一个抽象类
  2. 属性 System.MemberAccessException:无法创建 ScanAPI.SktScanProperty 的实例,因为它是一个抽象类

绑定工程中ISktScanObject接口的ApiDefinition如下:

[Protocol, Model]    
interface ISktScanObject {}

// @protocol ISktScanObject
[Protocol, Model]
[BaseType (typeof (NSObject))]
interface SktScanObject : ISktScanObject
{
    // @required -(id<ISktScanMsg>)Msg;
    [Abstract]
    [Export ("Msg")]
    ISktScanMsg Msg { get; }

    // @required -(id<ISktScanProperty>)Property;
    [Abstract]
    [Export ("Property")]
    SktScanProperty Property { get; }
} 

绑定项目中 SktClassFactory 接口的 ApiDefinition 如下:

[BaseType (typeof(NSObject))]
interface SktClassFactory
{
    // +(id<ISktScanObject>)createScanObject;
    [Static]
    [Export ("createScanObject")]
    ISktScanObject CreateScanObject ();

    // +(void)releaseScanObject:(id<ISktScanObject>)scanObj;
    [Static]
    [Export ("releaseScanObject:")]
    void ReleaseScanObject (ISktScanObject scanObj);

    // +(id<ISktScanApi>)createScanApiInstance;
    [Static]
    [Export ("createScanApiInstance")]
    ISktScanApi CreateScanApiInstance ();

    // +(void)releaseScanApiInstance:(id<ISktScanApi>)scanApi;
    [Static]
    [Export ("releaseScanApiInstance:")]
    void ReleaseScanApiInstance (ISktScanApi scanApi);

    // +(id<ISktScanDevice>)createDeviceInstance:(id<ISktScanApi>)scanApi;
    [Static]
    [Export ("createDeviceInstance:")]
    ISktScanDevice CreateDeviceInstance (ISktScanApi scanApi);

    // +(void)releaseDeviceInstance:(id<ISktScanDevice>)deviceInstance;
    [Static]
    [Export ("releaseDeviceInstance:")]
    void ReleaseDeviceInstance (ISktScanDevice deviceInstance);
} 

需要做什么才能正确实例化 _scanObjectReceived 对象。提前致谢。

4

2 回答 2

1

这可能早就应该了,但是使用当前的 API,我能够创建一个 iOS 绑定并使用以下方法调用 ISktScanObject 和 ISktScanApi:

ScanObject = SktClassFactory.CreateScanObject;
ScanApi = SktClassFactory.CreateScanApiInstance;

这没有给我关于抽象类的问题。

我的 SktClassFactory 的 ApiDefinition 如下所示:

// @interface SktClassFactory : NSObject
[BaseType (typeof(NSObject))]
interface SktClassFactory
{
    // +(ISktScanObject *)createScanObject;
    [Static]
    [Export ("createScanObject")]
    ISktScanObject CreateScanObject { get; }

    // +(void)releaseScanObject:(ISktScanObject *)scanObj;
    [Static]
    [Export ("releaseScanObject:")]
    void ReleaseScanObject (ISktScanObject scanObj);

    // +(ISktScanApi *)createScanApiInstance;
    [Static]
    [Export ("createScanApiInstance")]
    ISktScanApi CreateScanApiInstance { get; }

    // +(void)releaseScanApiInstance:(ISktScanApi *)scanApi;
    [Static]
    [Export ("releaseScanApiInstance:")]
    void ReleaseScanApiInstance (ISktScanApi scanApi);

    // +(ISktScanDevice *)createDeviceInstance:(ISktScanApi *)scanApi;
    [Static]
    [Export ("createDeviceInstance:")]
    ISktScanDevice CreateDeviceInstance (ISktScanApi scanApi);

    // +(void)releaseDeviceInstance:(ISktScanDevice *)deviceInstance;
    [Static]
    [Export ("releaseDeviceInstance:")]
    void ReleaseDeviceInstance (ISktScanDevice deviceInstance);
}

编辑:我的 ISktScanObject 在 ApiDefinition 中看起来像这样:

// @interface ISktScanObject : NSObject
[BaseType (typeof(NSObject))]
interface ISktScanObject
{
    // -(ISktScanMsg *)Msg;
    [Export ("Msg")]
    ISktScanMsg Msg { get; }

    // -(ISktScanProperty *)Property;
    [Export ("Property")]
    ISktScanProperty Property { get; }
}

希望这有任何帮助。

于 2015-11-23T16:53:01.007 回答
0

我看到您正在绑定一个@protocol,并且在 Philippes 的回答中,SDK 更改为使用@interface这种类型,所以他的回答并不清楚。

我通过为库无法实例化的返回类型添加骨架接口(空接口)并将所有返回类型和参数类型从类更改为接口名称来解决我的问题(使用另一个库)。

注意添加的接口和更改的返回类型。

// @protocol NXProximityZone <NSObject>
[Protocol, Model]
[BaseType(typeof(NSObject))]
interface NXProximityZone
{
    // Methods and properties removed for simplicity
}

// @interface NXProximityManager : NSObject
[BaseType(typeof(NSObject))]
interface NXProximityManager
{
    // +(NSObject<NXProximityZone> *)proximityZoneFromNotification:(NSNotification *)notification;
    [Static]
    [Export("proximityZoneFromNotification:")]
    NXProximityZone ProximityZoneFromNotification(NSNotification notification);

    // Methods and properties removed for simplicity
}

// @protocol NXProximityZone <NSObject>
[Protocol, Model]
[BaseType(typeof(NSObject))]
interface NXProximityZone
{
    // Methods and properties removed for simplicity
}

// Skeleton interface
interface INXProximityZone { }

// @interface NXProximityManager : NSObject
[BaseType(typeof(NSObject))]
interface NXProximityManager
{
    // +(NSObject<NXProximityZone> *)proximityZoneFromNotification:(NSNotification *)notification;
    [Static]
    [Export("proximityZoneFromNotification:")]
    INXProximityZone ProximityZoneFromNotification(NSNotification notification);

    // Methods and properties removed for simplicity
}

有关骨架接口的更多信息,请参见此处(搜索骨架页面)

于 2017-01-31T09:43:55.650 回答