【问题标题】:xamarin forms get pdf pathxamarin表单获取pdf路径
【发布时间】:2019-08-15 08:41:45
【问题描述】:

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

这是我的 C# 代码:

 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>

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

 [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 语句总是假的

【问题讨论】:

  • 你不能以同样的方式做这两个。平台之间的路径会有所不同,因此您需要针对每个平台进行实现。

标签: pdf xamarin.forms


【解决方案1】:

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

界面:

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;
        }
    }
}

在 Android 中:

[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 这是你的班级名称。

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

更新检查文件:,我在Android中添加了这个方法来检查文件是否存在于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

【讨论】:

  • 你的答案是完美的,除了我总是得到一个异常“对象引用未设置为对象的实例。”我猜这没有到达 pdf 文件,尽管当我尝试在 pdf 查看器中打开文件并为其提供相同的路径时,文件成功打开
  • @mohammadanouti 您在哪一行得到了这个异常?我猜你只在iOS上得到这个例外?你在用string pathBundle = Path.Combine(NSBundle.MainBundle.ResourcePath, fileName);吗?
  • 实际上,目前我正在 Android 上进行测试。我从 xamarin forms "string pdfPath = DependencyService.Get().filePath("myPdf.pdf");"并且在调试时它总是弹出这个并且没有通过“GetPdfFilePath_Android”类中的断点
  • @mohammadanouti 在您的 Android 类中添加 [assembly: Dependency(typeof(GetPdfFilePath_Android))]。在我的回答中查看我的编辑。
  • 是的,我应该已经注意到这个丢失了,谢谢!!
猜你喜欢
  • 2012-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-06
  • 2018-04-09
  • 1970-01-01
相关资源
最近更新 更多