Xamarin.forms 如何以Xamarin格式打印图像?

Xamarin.forms 如何以Xamarin格式打印图像?,xamarin.forms,xamarin.android,Xamarin.forms,Xamarin.android,我需要在Xamarin.Forms应用程序中打印图像。我知道,这对于每个平台都是不同的,所以它是这样的: if (Device.RuntimePlatform == Device.iOS) { //code for iOS (works) var printInfo = UIPrintInfo.PrintInfo; printInfo.JobName = "myJobN

我需要在Xamarin.Forms应用程序中打印图像。我知道,这对于每个平台都是不同的,所以它是这样的:

if (Device.RuntimePlatform == Device.iOS)
    {
                    //code for iOS (works)
                    var printInfo = UIPrintInfo.PrintInfo;
                    printInfo.JobName = "myJobName";
                    printInfo.OutputType = UIPrintInfoOutputType.General;

                    var printer = UIPrintInteractionController.SharedPrintController;
                    printer.PrintInfo = printInfo;
                    printer.PrintingItem = NSData.FromFile("imagePath");
                    printer.ShowsPageRange = true;

                    printer.Present(true, (handler, completed, error) =>
                    {
                        if (!completed && error != null)
                        {
                            Console.WriteLine(error.LocalizedDescription)
                        }
                    });

                    printInfo.Dispose();
    }
else   
    {
            //code for Android (does not work)
            DependencyService.Get<IPrint>().Print(myimageArray);
    }
以下是Android的打印类:

using System;
using System.IO;
using Android.Content;
using Android.Print;
using myApp.Interfaces;
using Xamarin.Forms;

namespace myApp.Droid
{
    public class Print : IPrint
    {
        [Obsolete]
        void IPrint.Print(byte[] content)
        {
            //Android print code goes here
            Stream inputStream = new MemoryStream(content);
            string fileName = "form.pdf";
            if (inputStream.CanSeek)
                //Reset the position of PDF document stream to be printed
                inputStream.Position = 0;
            //Create a new file in the Personal folder with the given name
            string createdFilePath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), fileName);
            //Save the stream to the created file
            using (var dest = System.IO.File.OpenWrite(createdFilePath))
                inputStream.CopyTo(dest);
            string filePath = createdFilePath;
            PrintManager printManager = (PrintManager)Forms.Context.GetSystemService(Context.PrintService);
            PrintDocumentAdapter pda = new CustomPrintDocumentAdapter(filePath);
            //Print with null PrintAttributes
            printManager.Print(fileName, pda, null);
        }
    }
}
这是CustomPrintDocumentAdapter:

using System;
using Android.OS;
using Android.Print;
using Java.IO;

namespace myApp.Droid
{
    public class CustomPrintDocumentAdapter : PrintDocumentAdapter
    {
        internal string FileToPrint { get; set; }

        internal CustomPrintDocumentAdapter(string fileDesc)
        {
            FileToPrint = fileDesc;
        }
        public override void OnLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras)
        {
            if (cancellationSignal.IsCanceled)
            {
                callback.OnLayoutCancelled();
                return;
            }


            PrintDocumentInfo pdi = new PrintDocumentInfo.Builder(FileToPrint).SetContentType(PrintContentType.Document).Build();

            callback.OnLayoutFinished(pdi, true);
        }

        public override void OnWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback)
        {
            InputStream input = null;
            OutputStream output = null;

            try
            {
                //Create FileInputStream object from the given file
                input = new FileInputStream(FileToPrint);
                //Create FileOutputStream object from the destination FileDescriptor instance
                output = new FileOutputStream(destination.FileDescriptor);

                byte[] buf = new byte[1024];
                int bytesRead;

                while ((bytesRead = input.Read(buf)) > 0)
                {
                    //Write the contents of the given file to the print destination
                    output.Write(buf, 0, bytesRead);
                }

                callback.OnWriteFinished(new PageRange[] { PageRange.AllPages });

            }
            catch (FileNotFoundException ex)
            {
                //Catch exception
                System.Diagnostics.Debug.WriteLine(ex);
            }
            catch (Exception e)
            {
                //Catch exception
                System.Diagnostics.Debug.WriteLine(e);
            }
            finally
            {
                try
                {
                    input.Close();
                    output.Close();
                }
                catch (IOException e)
                {
                    e.PrintStackTrace();
                    System.Diagnostics.Debug.WriteLine(e);
                }
            }
        }
    }
}

有人能帮我吗?提前感谢。

您应该将此添加到Android项目的打印类中

[assembly: Dependency(typeof(myApp.Droid. Print))]
像这样:

使用Xamarin.Forms; //添加这一行 [程序集:DependencytypeofmyApp.Droid.Print] 名称空间myApp.Droid { 公共类打印:IPrint { ... } }
哪些行导致了异常?您是否正确注册了依赖项服务?我看不到[assembly:DependencytypeofmyApp.Droid.Print]谢谢,现在它就像一个符咒。
[assembly: Dependency(typeof(myApp.Droid. Print))]