我正在为 Brother PJ-673 便携式打印机创建 MonoTouch 绑定。这将在 Xamarin.iOS iPad/iPhone 应用程序中使用。Brother 提供了一个 Objective-C 的 SDK,可以在http://www.brother.com/product/dev/mobile/ios/download/index.htm找到
首先,我使用了 Xamarin MonoTouch Binding Objective-C 指南并创建了一个似乎工作正常的绑定项目。下面是我的 ApiDefinition.cs 文件的一瞥(使用 Objective Sharpie 作为文件的初稿,然后手动更改)
using System;
using System.Drawing;
using MonoTouch.CoreGraphics;
using MonoTouch.ObjCRuntime;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace BrotherTouchPrinterBinding
{
[BaseType (typeof (NSObject), Name = "BRPtouchNetworkInfo")]
public partial interface BrotherPrintTouchNetworkInfo {
[Export ("strIPAddress", ArgumentSemantic.Copy)]
string IPAddress { get; set; }
[Export ("strLocation", ArgumentSemantic.Copy)]
string Location { get; set; }
[Export ("strModelName", ArgumentSemantic.Copy)]
string ModelName { get; set; }
[Export ("strSerialNumber", ArgumentSemantic.Copy)]
string SerialNumber { get; set; }
[Export ("strNodeName", ArgumentSemantic.Copy)]
string NodeName { get; set; }
[Export ("strMACAddress", ArgumentSemantic.Copy)]
string MACAddress { get; set; }
}
[BaseType (typeof (NSObject), Name = "BRPtouchNetwork",
Delegates = new string[] {"WeakDelegate"},
Events = new Type [] { typeof (BrotherPrintTouchNetworkDelegate) })]
public partial interface BrotherPrinterTouchNetwork {
[Export ("startSearch:")]
int StartSearch (int nTimeout);
[Export ("getPrinterNetInfo:")]
NSObject [] GetPrinterNetInfo ();
[Export ("initWithPrinterName:")]
void InitWithPrinterName (string strPrinterName);
[Export ("setPrinterName:")]
bool SetPrinterName (string strPrinterName);
[Wrap ("WeakDelegate")][NullAllowed]
BrotherPrintTouchNetworkDelegate Delegate { get; set; }
[Export("delegate")][NullAllowed]
NSObject WeakDelegate { get; set; }
}
[BaseType (typeof (NSObject), Name = "BRPtouchNetworkDelegate")]
[Protocol]
[Model]
public partial interface BrotherPrintTouchNetworkDelegate {
[Export ("didFinishedSearch:")]
void DidFinishedSearch (NSObject sender);
}
[BaseType (typeof (NSObject), Name = "BRPtouchPrintInfo" )]
public partial interface BrotherPrintTouchPrintInfo {
[Export ("strPaperName", ArgumentSemantic.Retain)]
string PaperName { get; set; }
[Export ("ulOption")]
uint Option { get; set; }
[Export ("nPrintMode")]
int PrintMode { get; set; }
[Export ("nDensity")]
int Density { get; set; }
[Export ("nOrientation")]
int Orientation { get; set; }
[Export ("nHalftone")]
int Halftone { get; set; }
[Export ("nHorizontalAlign")]
int HorizontalAlign { get; set; }
[Export ("nVerticalAlign")]
int VerticalAlign { get; set; }
[Export ("nPaperAlign")]
int PaperAlign { get; set; }
[Export ("nExtFlag")]
int ExtFlag { get; set; }
[Export ("nAutoCutFlag")]
int AutoCutFlag { get; set; }
[Export ("nAutoCutCopies")]
int AutoCutCopies { get; set; }
}
[BaseType (typeof (NSObject), Name = "BRPtouchPrinterData")]
public partial interface BrotherPrintTouchPrinterData {
[Export ("getPrinterList:")]
NSObject [] GetPrinterList ();
}
[BaseType (typeof (NSObject), Name = "BRPtouchPrinter")]
public partial interface BrotherPrintTouchPrinter {
[Export ("setIPAddress:")]
void SetIPAddress (string ipAddress);
[Export ("sendFile::")]
int SendFile (string strFile, int nTimeout);
[Export ("sendData::")]
int SendData (NSData dataData, int nTimeout);
[Export ("sendFileEx:timeout:")]
int SendFileEx (string strFile, int nTimeout);
[Export ("sendDataEx:timeout:")]
int SendDataEx (NSData dataData, int nTimeout);
[Export ("initWithPrinterName:")]
IntPtr Constructor (string strPrinterName);
[Export ("setPrinterName:")]
bool SetPrinterName (string strPrinterName);
[Export ("isPrinterReady:")]
bool IsPrinterReady ();
[Export ("getPTStatus:")]
int GetPtStatus (PTSTATUS status);
[Export ("printImage:copy:timeout:")]
int PrintImage (CGImage imageRef, int nCopy, int nTimeout);
[Export ("setPrintInfo:")]
int SetPrintInfo (BrotherPrintTouchPrintInfo printInfo);
[Export ("setCustomPaperFile:")]
bool SetCustomPaperFile (string strFilePath);
[Export ("setEncryptKey:keyEx:")]
bool SetEncryptKey (string strKey, string strKeyEx);
[Export ("startPrint:")]
int StartPrint ();
[Export ("endPrint:")]
void EndPrint ();
}
}
MonoTouch 绑定项目编译成功,接下来我创建了一个小型测试应用程序。下面的代码是我如何创建相关 Brother 类型的本机实例:
partial void PrintTouchDown (NSObject sender)
{
// Initialize printer settings
BrotherPrintTouchPrintInfo printInfo = new BrotherPrintTouchPrintInfo();
printInfo.PaperName = "A4_CutSheet";
printInfo.PrintMode = (int)PrintMode.PRINT_FIT;
printInfo.Density = 5;
printInfo.Orientation = (int)Orientation.ORI_PORTRATE;
printInfo.Halftone = (int)Halftone.HALFTONE_ERRDIF;
printInfo.HorizontalAlign = (int)HorizontalAlign.ALIGN_CENTER;
printInfo.VerticalAlign = (int)VerticalAlign.ALIGN_MIDDLE;
printInfo.PaperAlign = (int)PaperAlign.PAPERALIGN_LEFT;
printInfo.ExtFlag |= 0;
// Initialize PJ-673 printer
BrotherPrintTouchPrinter printer = new BrotherPrintTouchPrinter("Brother PJ-673");
printer.SetIPAddress("10.20.2.104");
printer.SetPrintInfo(printInfo);
//printer.PrintImage(selectedImage.CGImage, 1, 1000);
}
一切正常,直到调用printer.SetPrinterInfo(printInfo)
这是应用程序崩溃的地方,无一例外。应用程序输出为:
mono-rt: Stacktrace:
mono-rt: at <unknown> <0xffffffff>
mono-rt: at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging.int_objc_msgSend_IntPtr (intptr,intptr,intptr) <IL 0x00027, 0xffffffff>
mono-rt: at BrotherTouchPrinterBinding.BrotherPrintTouchPrinter.SetPrintInfo (BrotherTouchPrinterBinding.BrotherPrintTouchPrintInfo) <IL 0x0002f, 0x000df>
mono-rt: at BrotherTouchPrinter.iOS.BrotherTouchPrinter_iOSViewController.PrintTouchDown (MonoTouch.Foundation.NSObject) [0x00069] in /Users/claudiu/Projects/BrotherTouchPrinterBinding/BrotherTouchPrinter.iOS/BrotherTouchPrinter.iOSViewController.cs:82
mono-rt: at (wrapper runtime-invoke) <Module>.runtime_invoke_void__this___object (object,intptr,intptr,intptr) <IL 0x00052, 0xffffffff>
mono-rt: at <unknown> <0xffffffff>
mono-rt: at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <IL 0x0009f, 0xffffffff>
mono-rt: at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
mono-rt: at BrotherTouchPrinter.iOS.Application.Main (string[]) [0x00008] in /Users/claudiu/Projects/BrotherTouchPrinterBinding/BrotherTouchPrinter.iOS/Main.cs:16
mono-rt: at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff
mono-rt:
Native stacktrace:
mono-rt:
=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
=================================================================
非常感谢解决此问题的任何帮助。我对 Xamarin 开发很陌生,我不确定我能做些什么来解决这个问题。我很确定这与我的 MonoTouch 绑定有关,因为 Brother SDK 包含一个用 Objective-C 编写的工作示例应用程序,所以我认为库本身没有问题。
谢谢