首页 > 解决方案 > 适用于 Android 和 iOS 应用程序的静音打印功能

问题描述

我正在使用 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 应用程序静默打印?

标签: androidiosxamarinprintingxamarin.android

解决方案


推荐阅读