c# - Monotouch 绑定(bind)到 Linea Pro SDK

标签 c# ios binding xamarin.ios linea-pro

我正在尝试创建与 Linea Pro(这是他们在 Apple Stores、Lowes 中使用的条形码扫描仪)SDK 的绑定(bind)。我正在使用David Sandor's绑定(bind)作为引用,但自 2011 年 1 月以来 SDK 已经更新了几次。

除了 playSound 调用(用于在 Linea Pro 设备上播放声音)之外,我几乎一切都正常工作。

来自 SDK 的 .h 文件的调用如下:

-(BOOL)playSound:(int)volume beepData:(int *)data length:(int)length error:(NSError **)error;

我尝试过使用 int[]、NSArray 和 int[] 的 IntPtr,但似乎没有任何效果。

我的绑定(bind)的最后一次不成功的迭代如下所示:

[Export ("playSound:beepData:length:")]
void PlaySound (int volume, NSArray data, int length);

现在,这根本不起作用。另请注意,我也不知道如何处理 error:(NSError **)error 部分。

我对 C 缺乏一定的了解,因此我们将非常感谢任何帮助。

最佳答案

除非 Objective-C 代码使用 NSArray,否则不能使用 NSArray,即生成器允许我们将一些 ObjC 构造映射到 .NET 类型(例如 NSString string),但它不允许您重新定义 ObjC 类型。

-(BOOL)playSound:(int)volume beepData:(int *)data length:(int)length error:(NSError **)error;

应该是这样的:

[Export ("playSound:beepData:length:error:")]
bool PlaySound (int volume, IntPtr data, int length, out NSError error);

您需要将数据编码到IntPtr中。

IntPtr data = Marshal.AllocHGlobal (length);
Marshal.WriteInt32 (data1, 0);

然后释放它。

Marshal.FreeHGlobal (data);

最好使用公共(public)辅助方法来调用您的内部绑定(bind)。您可以通过在定义中添加 [Internal] 属性来将 PlaySound 方法设置为 internal。所以就变成了:

[Export ("playSound:beepData:length:error:")][Internal]
bool PlaySound (int volume, IntPtr data, int length, out NSError error);

并且您在绑定(bind)中包含以下代码(例如 API.cs):

bool PlaySound (int volume, int[] data)
{
    // I assume length is byte-based (check the docs)
    int length = data.Length * 4; 
    IntPtr p = Marshal.AllocHGlobal (length);
    int j = 0;
    for (int i=0; i < length; i+=4)
        Marshal.WriteInt32 (p [j++], i);
    NSError error;
    bool result = PlaySound (volume, p, length, out error);
    // free memory before throwing the exception (if any)
    Marshal.FreeHGlobal (data);
    if (error != null)
       throw new Exception (error.LocalizedDescription);
    return result;
}

注意:完全没有尝试过:-) 我没有硬件、SDK 或文档。 YMMV 但应该很接近。

关于c# - Monotouch 绑定(bind)到 Linea Pro SDK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12442740/

相关文章:

c# - 包含异常作为 DataContractSerializer WCF C# 的 DataMember

ios - 为什么 customTableViewCell 不同且速度慢?

ios - 如何比较两个线性渐变? swift 用户界面

ios - 从初始值开始 UIStepper

c# 将类属性绑定(bind)到复杂结构

WPF 元素绑定(bind)在 XAML 中不起作用

c# - 将 DataTable 分配给 DataGridView 时的 "NullReferenceException was unhandled"

c# - ASP.NET 自定义登录

javascript - Uncaught TypeError : _this2. props.selectBook 不是函数,自动复制这个

c# - C# 中的 select() 函数