我正在为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绑定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编写的工作示例应用程序,因此我认为库本身没有问题。

谢谢

最佳答案

您提供的stacktrace显示:


mono-rt:位于BrotherTouchPrinterBinding.BrotherPrintTouchPrinter.PrintImage
(MonoTouch.CoreGraphics.CGImage,int,int)


这是:


调用printer.SetPrinterInfo(printInfo)之后,因此它似乎不会在那里崩溃
在您的来源中注释了一行。


您确定源代码(上面)与堆栈跟踪匹配吗?

还是您是说崩溃发生的时间较晚(在打印时)但仅在调用printer.SetPrinterInfo(printInfo)时发生?

注意:您可以编辑问题以添加/更新信息

关于objective-c - 将Brother PJ-673 SDK(Objective-C)绑定(bind)到MonoTouch,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18253683/

10-12 04:45