xamarin 表单获取 pdf 路径 [英] xamarin forms get pdf path

查看:41
本文介绍了xamarin 表单获取 pdf 路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功从项目中读取 pdf 并显示在 WebView 中.我想要做的是从我的项目中获取 pdf 文件路径并检查它是否存在以显示它,如果不存在,则为其提供另一个 URI 以打开它.任何人都知道我如何获得 pdf 的路径,它适用于 Android 和 iOS.

I've successfully read a pdf from the project and displayed in a WebView. What I want to do is to get the pdf file path from my project and check if it exists to display it, and if not, to give it another URI to open with. Anyone knows how I can get the path of the pdf and it works for both Android and iOS.

这是我的 C# 代码:

Here is my C# code:

 string internalstorageurl = string.Format("file:///android_asset/Content/{0}", WebUtility.UrlEncode("Conctionprofile.pdf"));
        public pdfviewer ()
        {
            InitializeComponent ();
            PDFReading();
        }
        private void PDFReading()
        {

                pdfwebview.Uri = internalstorageurl;

        }

XAML:

    <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:AppXamarin.CustomRenders"
             x:Class="AppXamarin.Pages.pdfviewer"
             Padding="0,20,0,0">
    <ContentPage.Content>
        <local:CustomWebView x:Name="pdfwebview" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
    </ContentPage.Content>
</ContentPage>

我正在使用以下代码检查该文件是否存在于我的项目中:

I'm checking if the file exist in my project with this code:

 [assembly: Dependency(typeof(GetPdfFilePath_Android))]
namespace AppXamarin.Droid.CustomRender
{
    public class GetPdfFilePath_Android : Java.Lang.Object, IGetPdfFilePath
    {
        public string filePath(string fileName)
        {      
                string internalstorageurl = string.Format("file:///android_asset/Content/{0}", WebUtility.UrlEncode(fileName));
            if (File.Exists(internalstorageurl))
                return internalstorageurl;
            else
                return "not found";
        }
    }
}

if 语句总是错误的

推荐答案

您可以使用 dependencyService 从 iOS 和 Android 应用程序获取 pdf 文件路径.

You can use dependencyService to get pdf file path from both iOS and Android app.

界面:

public interface IGetPdfFilePath
{
    string filePath(string fileName);
}

获取 Xamarin.forms 中的文件路径:

string pdfPath = DependencyService.Get<IGetPdfFilePath>().filePath("myPdf.pdf");
                Console.WriteLine(pdfPath);

在 iOS 中,路径取决于您存储文件的位置:

[assembly: Dependency (typeof (GetPdfFilePath_iOS))]

namespace UsingDependencyService.iOS
{
    public class GetPdfFilePath_iOS : IGetPdfFilePath
    {
        public GetPdfFilePath_iOS ()
        {
        }

        public string filePath(string fileName)
        {
            // 1. if your store your pdf in DocumentDirectory
            //string directories = NSSearchPath.GetDirectories(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.All)[0];
            //string pathBundle = directories + "/" + fileName;
            ////bool fileExists = NSFileManager.DefaultManager.FileExists(pathBundle);
            //return pathBundle;

            //2.pdf file in your project
            string pathBundle = Path.Combine(NSBundle.MainBundle.ResourcePath, fileName);

            return pathBundle;
        }
    }
}

在安卓中:

[assembly: Dependency(typeof(GetPdfFilePath_Android))]
namespace UsingDependencyService.Android
{
    public class GetPdfFilePath_Android : Java.Lang.Object, IGetPdfFilePath
    {

        public string filePath(string fileName)
        {

            string internalstorageurl = string.Format("file:///android_asset/Content/{0}", WebUtility.UrlEncode(fileName));

            return internalstorageurl;

        }
    }
}

更新:

将此行:[assembly: Dependency(typeof(GetPdfFilePath_Android))] 添加到您的 Android 类.GetPdfFilePath_Android 这里是你的类名.

Add this line: [assembly: Dependency(typeof(GetPdfFilePath_Android))] to your Android class. GetPdfFilePath_Android here is your class name.

另一种方法是为你的 webview 创建一个自定义渲染器,你可以看看这个 项目.

Another way is to create a custom renderer of your webview, you can have a look at this project.

更新检查文件:,我在Android中添加了这个方法来检查文件是否存在于Assets/Content.

Update to check file:, I add this method in the Android to check if the file exist inside Assets/Content.

public bool fileExist (string fileName) {

            AssetManager assets = MainActivity.Instance.Assets;        
            string[] names = assets.List("Content");

            for (int i = 0; i < names.Length; i++)
            {
                if (names[i] == fileName)
                {
                    return true;
                }
            }

            return false;
        }

注意:如果在 Assets 中找不到文件,则必须将 pdf 文件的构建操作更改为 AndroidAsset.

Note: You have to change the build action of pdf file to AndroidAsset if you can't find the file inside Assets.

这篇关于xamarin 表单获取 pdf 路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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