I'm working on an iPad app in Monotouch (5.2.12). The app will be used with Zebra's mobile printers, so we got SDK from them (.a library and headers). So I went to read all the guides and tutorials and created a bindings project for it (just 2 connection classes). I was really excited to get it working quickly for basic text and label printing.
But we'll need to print PDFs. To do that, I need to bind more classes and I just cannot figure out how for 2 days now. Here's the general setup of the library:
ZebraPrinterConnection protocol is implemented by TcpPrinterConnection interface. ZebraPrinterFactory interface is used to obtain an instance of ZebraPrinter protocol and requires ZebraPrinterConnection to be passed to it.
Here's the core of the bindings:
Header (.h)
@protocol ZebraPrinterConnection
- (BOOL) open;
- (void) close;
- (NSInteger) write:(NSData *)data error:(NSError **)error;
- (NSData *)read: (NSError**)error;
Binding (.cs)
[BaseType (typeof (NSObject))]
[Model]
interface ZebraPrinterConnection {
[Export ("open")]
bool Open();
[Export ("close")]
void Close();
[Export ("write:error:")]
int Write(NSData data, out NSError error);
[Export ("read:")]
NSData Read(out NSError error);
}
Header (.h)
@interface TcpPrinterConnection : NSObject<ZebraPrinterConnection>
- (id)initWithAddress:(NSString *)anAddress andWithPort:(NSInteger)aPort;
Binding (.cs)
[BaseType (typeof(ZebraPrinterConnection))]
interface TcpPrinterConnection {
[Export ("initWithAddress:andWithPort:")]
IntPtr Constructor (string anAddress, int aPort);
}
Header (.h)
@interface ZebraPrinterFactory : NSObject
+(id<ZebraPrinter,NSObject>) getInstance:(id<ZebraPrinterConnection, NSObject>)
connection error:(NSError**)error
Binding (.cs)
[BaseType (typeof(NSObject))]
interface ZebraPrinterFactory {
[Static, Export ("getInstance:error:")]
ZebraPrinter getInstance(ZebraPrinterConnection connection, out NSError error);
}
Note how ZebraPrinterFactory wants ZebraPrinterConnection
to be passed to it, but only TcpPrinterConnection
has an actual constructor.
If I try to use something like:
NSError err;
TcpPrinterConnection conn;
conn = new TcpPrinterConnection(ipAddress, port);
bool connectionOK = conn.Open ();
ZebraPrinter zPrinter = ZebraPrinterFactory.getInstance(conn, out err);
then I get a "System.InvalidCastException: Cannot cast from source type to destination type." at runtime...
It's a terrible feeling knowing that you ALMOST got it working, but not quite... How does one get around this?
UPDATE: OK, I removed the ZebraPrinterConnection class from the binding entirely, copying its methods into TcpPrinterConnection (as suggested by Jonathan). Still no luck (same exception). Then bound another class that has methods that expect ZebraPrinterConnection as a parameter and this one works smooth as silk.
Header (.h)
@interface SGD : NSObject {}
+(NSString*) GET: (NSString*) setting
withPrinterConnection: (id<ZebraPrinterConnection,NSObject>) printerConnection
error:(NSError**)error;
Binding (.cs)
[BaseType (typeof (NSObject))]
interface SGD
{
[Static, Export ("GET:withPrinterConnection:error:")]
string Get (string setting, TcpPrinterConnection printerConnection, out NSError error);
}
I am starting to suspect the implementation of ZebraPrinterFactory class being root of the problem, now that other classes can be bound without any issues whatsoever. On the other hand, it might have something to do with the returned instance of ZebraPrinter class. Could it be that Mono cannot map ZebraPrinter to the thing being returned by the factory class?
I'm not familiar with MonoTouch, but know about the zebra apis. Hopefully some background of the way the sdk works will help with these mono mappings
So, the ZebraPrinterFactory is a simple factory which interrogates a printer connection to figure out which model of printer you have (ZPL or CPCL). The return type of the factory is an Interface, ZebraPrinter
. The concrete type of a ZebraPrinter
is an internal, non documented class, ZebraPrinterZpl
or ZebraPrinterCpcl
. Both of these concrete classes conform to the ZebraPrinter
interface and can perform functions on a printer. We chose to do this to abstract away the need to know about printer languages. But if Mono needs to know about a concrete class to perform the mapping, you can cast the return to the concrete class ZebraPrinterCpcl
or ZebraPrinterZpl
(depending on your printer model, small mobile printers are probably cpcl, large desktops or tabletops are ZPL)
You can alternatively bypass the factory by instatiating a concrete type of printer by just calling its constructor (unfortunately, it is not documented, because the factory is the preferred way to do this) But it would be something like this:
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);
Hope this helps!