Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8106401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:27:12+00:00 2026-06-06T00:27:12+00:00

I’m working on an iPad app in Monotouch (5.2.12). The app will be used

  • 0

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:

ZebraPrinterConnection

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);
    }

TcpPrinterConnection

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);
}       

ZebraPrinterFactory

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);
}

The problem:

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T00:27:14+00:00Added an answer on June 6, 2026 at 12:27 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.