Binding Zebra SDK与Monotouch的绑定?

Binding Zebra SDK与Monotouch的绑定?,binding,sdk,xamarin.ios,zebra-printers,Binding,Sdk,Xamarin.ios,Zebra Printers,我正在用Monotouch(5.2.12)开发一款iPad应用程序。该应用程序将与Zebra的移动打印机一起使用,因此我们从他们那里获得了SDK(.a库和标题)。因此,我阅读了所有的指南和教程,并为其创建了一个绑定项目(仅2个连接类)。我真的很兴奋能让它快速用于基本文本和标签打印 但我们需要打印PDF。要做到这一点,我需要绑定更多的类,但我现在已经两天不知道如何绑定了。以下是库的一般设置: ZebraPrinterConnection协议由TCPPR互连接口实现。 ZebraPrinterFac

我正在用Monotouch(5.2.12)开发一款iPad应用程序。该应用程序将与Zebra的移动打印机一起使用,因此我们从他们那里获得了SDK(.a库和标题)。因此,我阅读了所有的指南和教程,并为其创建了一个绑定项目(仅2个连接类)。我真的很兴奋能让它快速用于基本文本和标签打印

但我们需要打印PDF。要做到这一点,我需要绑定更多的类,但我现在已经两天不知道如何绑定了。以下是库的一般设置:

ZebraPrinterConnection协议由TCPPR互连接口实现。 ZebraPrinterFactory接口用于获取ZebraPrinter协议的实例,并要求向其传递ZebraPrinterConnection

以下是绑定的核心:

斑马线连接 标题(.h)

绑定(.cs)

TCPPR互连 标题(.h)

ZebraPrinterFactory 标题(.h)

问题是: 请注意ZebraPrinterFactory希望如何将
ZebraPrinterConnection
传递给它,但只有
TcprInterconnection
具有实际的构造函数

如果我尝试使用以下内容:

NSError err;
TcpPrinterConnection conn;
conn = new TcpPrinterConnection(ipAddress, port);
bool connectionOK = conn.Open ();
ZebraPrinter zPrinter = ZebraPrinterFactory.getInstance(conn, out err); 
然后我得到一个“System.InvalidCastException:无法从源类型强制转换到目标类型”

这是一种可怕的感觉,知道你几乎让它工作,但不完全。。。你是如何避开这一点的

更新:好的,我完全从绑定中删除了ZebraPrinterConnection类,将其方法复制到TcpPrinterConnection中(如Jonathan所建议的)。仍然没有运气(同样的例外)。然后绑定另一个类,该类的方法期望ZebraPrinterConnection作为参数,而这个类的工作非常平滑

标题(.h)

我开始怀疑ZebraPrinterFactory类的实现是问题的根源,现在其他类可以绑定而没有任何问题。另一方面,它可能与ZebraPrinter类的返回实例有关。是否Mono无法将ZebraPrinter映射到factory类返回的对象?

而不是:

[BaseType (typeof(NSObject))]
interface TcpPrinterConnection : ZebraPrinterConnection {  
使用:


然后绑定项目应该正确设置继承。
BaseType
属性确定基类,而不是实际继承接口(我知道这有点奇怪)。

我不熟悉MonoTouch,但了解zebra API。希望sdk工作方式的一些背景知识能对这些mono映射有所帮助

因此,ZebraPrinterFactory是一个简单的工厂,它通过查询打印机连接来确定您使用的打印机型号(ZPL或CPCL)。工厂的返回类型是一个接口,
ZebraPrinter
ZebraPrinter
的具体类型是一个内部的、无文档记录的类,
ZebraPrinterZpl
ZebraPrinterPCL
。这两个具体类都符合ZebraPrinter接口,可以在打印机上执行功能。我们选择这样做是为了抽象出了解打印机语言的需要。但是,如果Mono需要了解具体类以执行映射,则可以将返回转换为具体类
ZebraPrinterPCL
ZebraPrinterZpl
(根据打印机型号,小型移动打印机可能是cpcl,大型台式机或台式机可能是ZPL)

您也可以通过调用具体类型的打印机的构造器(不幸的是,它没有文档记录,因为工厂是实现这一点的首选方式)来安装具体类型的打印机,从而绕过工厂,但它是这样的:

ZebraPrinterCpcl myPrinter = new ZebraPrinterCpcl(conn);  
//or using the base interface as the type... and you may need to pass in an NSError also, I forget now...
ZebraPrinter myPrinter = new ZebraPrinterCpcl(conn);

希望这有帮助

谢谢你的回答,我明天会试试(现在是晚上10点,我几个小时前就放弃了)。如果我没记错的话,我试过类似的方法,结果还是相同的运行时异常。不幸的是,乔纳森,你上面的回答没有帮助。我更新了绑定,必须从基类中删除[Model]属性,否则它会在TcpPrinterConnection.Open()上出现“您不应该在这个方法中调用”异常。之后,当我尝试使用factory类时,它会给我运行时异常“System.InvalidCastException:无法从源类型强制转换到目标类型”。此外,在调试器中检查TCPPR互连实例会立即使应用程序崩溃。检查崩溃已解决,与绑定TCPPR互连类的私有属性有关。尽管如此,仍然存在无效强制转换异常。这是否意味着MonoTouch运行时无法将一种类型转换为另一种类型,尽管第一种类型是从第二种类型派生的?您肯定不需要使用
Model
属性。您是否尝试过完全删除基本接口?只需为
TcpConnection
创建一个绑定,并将基类中的所有方法也放在其中。这应该是可行的,因为Obj-C消息传递的工作方式。只是从绑定中删除了基本ZebraPrinterConnection类,将其所有方法复制到TCPPR互连中。相同的无效强制转换异常。有趣的是,我绑定了另一个类,该类希望获得ZebraPrinterConnection作为参数,它可以工作<代码>(NSString*)获取:(NSString*)设置与打印机连接:(id)打印机连接错误:(NSError**)错误绑定为
[静态,导出(“SET:withValue:andWithPrinterConnection:error:”)]布尔集(字符串设置,字符串值,TCPPR打印机连接,输出NSError错误)确实有用!奥蒂斯勒,你太棒了!在Zebra支持论坛上,我确实发现有人提到了Android SDK的ZebraPrinterCcl类,但我没有意识到这对我也有帮助。当然,另一件事是Monotouch拒绝执行
zPrinter.getGraphicsUtil()
,但有相同的例外。我假设有具体的类实现GraphicsUtil协议,并应用相同的逻辑bind
[BaseType (typeof(ZebraPrinterConnection))]
interface TcpPrinterConnection {        
    [Export ("initWithAddress:andWithPort:")]
    IntPtr Constructor (string anAddress, int aPort);
}       
@interface ZebraPrinterFactory : NSObject
  +(id<ZebraPrinter,NSObject>) getInstance:(id<ZebraPrinterConnection, NSObject>) 
    connection error:(NSError**)error
[BaseType (typeof(NSObject))]
interface ZebraPrinterFactory {
  [Static, Export ("getInstance:error:")]
  ZebraPrinter getInstance(ZebraPrinterConnection connection, out NSError error);
}
NSError err;
TcpPrinterConnection conn;
conn = new TcpPrinterConnection(ipAddress, port);
bool connectionOK = conn.Open ();
ZebraPrinter zPrinter = ZebraPrinterFactory.getInstance(conn, out err); 
@interface SGD : NSObject {}
  +(NSString*) GET: (NSString*) setting 
    withPrinterConnection: (id<ZebraPrinterConnection,NSObject>) printerConnection 
    error:(NSError**)error;
[BaseType (typeof (NSObject))]
interface SGD 
{
  [Static, Export ("GET:withPrinterConnection:error:")]
  string Get (string setting, TcpPrinterConnection printerConnection, out NSError error);
}
[BaseType (typeof(NSObject))]
interface TcpPrinterConnection : ZebraPrinterConnection {  
[BaseType (typeof(ZebraPrinterConnection ))]
interface TcpPrinterConnection { 
ZebraPrinterCpcl myPrinter = new ZebraPrinterCpcl(conn);  
//or using the base interface as the type... and you may need to pass in an NSError also, I forget now...
ZebraPrinter myPrinter = new ZebraPrinterCpcl(conn);