如何在 Xamarin Forms 中加载 PDF [英] How to load PDF in Xamarin Forms

查看:31
本文介绍了如何在 Xamarin Forms 中加载 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Xamarin Forms 应用程序,我想在其中打开本地存储的 PDF.我不需要在应用程序中加载它们,我可以使用设备的默认文档查看器查看 PDF.我该怎么做?

I have a Xamarin Forms app where I want to open a locally stored PDF. I don't need to load them within the app, I'm fine with shelling out to the device's default document viewer for PDFs. How can I do this?

我尝试将 WebView 发送到 PDF,但是没有用,我得到了一个空白页面.

I tried sending a WebView to the PDF, but that didn't work, I just got a blank page.

推荐答案

我最近在我自己的项目中使用自定义渲染器完成了这项工作.首先实现一个空的 Xamarin 表单视图,例如(我已经包含了一个可绑定的 FilePath 属性):

I've recently done this in my own project using a custom renderer. First implement an empty Xamarin forms view such as (I've included a bindable FilePath attribute):

    public class PdfViewer : View
{
    public static readonly BindableProperty FilePathProperty =
        BindableProperty.Create<DocumentViewer, string>(p => p.FilePath, null);

    public string FilePath
    {
        get
        {
            return (string)this.GetValue(FilePathProperty);
        }

        set
        {
            this.SetValue(FilePathProperty, value);
        }
    }
}

然后创建一个将为该控件注册的iOS Renderer.该渲染器可以在 iOS 项目中使用 Quick Look Preview Controller 委托给内置的 iOS pdf 查看器:

Then create an iOS Renderer that will be registered for this control. This renderer can, as it is within an iOS project, use the Quick Look Preview Controller to delegate to the built in iOS pdf viewer:

[assembly: ExportRenderer(typeof(PdfViewer), typeof(DocumentViewRenderer))]

public class DocumentViewRenderer 
        : ViewRenderer<PdfViewer, UIView>
{
    private QLPreviewController controller;

    protected override void OnElementChanged(ElementChangedEventArgs<DocumentViewer> e)
    {
        base.OnElementChanged(e);

        this.controller = new QLPreviewController();
        this.controller.DataSource = new DocumentQLPreviewControllerDataSource(e.NewElement.FilePath);

        SetNativeControl(this.controller.View);
    }

    private class DocumentQLPreviewControllerDataSource : QLPreviewControllerDataSource 
    {
        private string fileName;
        public DocumentQLPreviewControllerDataSource(string fileName)
        {
            this.fileName = fileName;
        }

        public override int PreviewItemCount(QLPreviewController controller) 
        {
            return 1;
        }

        public override QLPreviewItem GetPreviewItem(QLPreviewController controller, int index)
        {
            var documents = NSBundle.MainBundle.BundlePath;
            var library = Path.Combine(documents, this.fileName);
            NSUrl url = NSUrl.FromFilename(library);

            return new QlItem(string.Empty, url);
        }

        private class QlItem : QLPreviewItem 
        { 
            public QlItem(string title, NSUrl uri) 
            { 
                this.ItemTitle = title; 
                this.ItemUrl = uri; 
            } 

            public override string ItemTitle { get; private set; }

            public override NSUrl ItemUrl { get; private set; }
        }
    }
}

我还没有编译和运行它,因为我已经从我的大项目中提取了它,但总的来说这应该可以工作.

I haven't compiled and run this as I've extracted it from my larger project but in general this should work.

这篇关于如何在 Xamarin Forms 中加载 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆