适用于Android和iOS应用程序的静音打印功能

时间:2019-07-31 06:10:45

标签: android ios xamarin printing xamarin.android

我正在使用Xamarin在iOS和Android应用程序上工作,目前我正在尝试使用该工具实现无声打印功能,我正在使用以下Android本机脚本,这些脚本始终在打印过程中显示对话框。

使用Webview的Android打印脚本:

public void Print(WebView viewToPrint)
    {
        var droidViewToPrint = Platform.CreateRenderer(viewToPrint).ViewGroup.GetChildAt(0) as Android.Webkit.WebView;

        if (droidViewToPrint != null)
        {
            // Only valid for API 19+
            var version = Android.OS.Build.VERSION.SdkInt;

            if (version >= Android.OS.BuildVersionCodes.Kitkat)
            {
                var printMgr = (PrintManager)Forms.Context.GetSystemService(Context.PrintService);

                printMgr.Print("Forms-EZ-Print", droidViewToPrint.CreatePrintDocumentAdapter(), null);

            }
        }
    }

用于文本的iOS打印脚本:

public void Print()
 {
     var printInfo = UIPrintInfo.PrintInfo;
     printInfo.JobName = "My first Print Job";
     printInfo.OutputType = UIPrintInfoOutputType.General;

     var textFormatter = new UISimpleTextPrintFormatter("Once upon a time...")
     {
        StartPage = 0,
        MaximumContentWidth = 6 * 72,
        PerPageContentInsets = new UIEdgeInsets(72, 72, 72, 72),
     };

     var printer = UIPrintInteractionController.SharedPrintController;
     printer.PrintInfo = printInfo;
     printer.PrintFormatter = textFormatter;
     printer.ShowsPageRange = true;
     printer.Present(true, (handler, completed, error) =>
     {
        if (!completed && error != null)
        {
         Console.WriteLine($"Error: {error.LocalizedDescription ?? ""}");
        }
        });

    printInfo.Dispose();
    textFormatter.Dispose();
}

能否让我知道如何在没有任何打印预览或对话框的情况下从iOS和Android应用程序静默打印?

0 个答案:

没有答案