【问题标题】:display local PDF file in webview control - Displays Blank Pdf File在 webview 控件中显示本地 PDF 文件 - 显示空白 Pdf 文件
【发布时间】:2017-11-26 02:50:15
【问题描述】:

我正在开发 Xamarin 表单 - UWP。 我想在 webview 控件中显示本地 PDF 文件。我关注了这两个链接:- https://developer.xamarin.com/recipes/cross-platform/xamarin-forms/controls/display-pdf/

Xamarin Forms UWP - Display PDF

它会打开 pdf 文件,但内容都是空白的。 谁能帮我解决我可能缺少的问题?

提前致谢!

这是我的代码:- CustomWebView.cs

public class CustomWebView : WebView
    {
        public static readonly BindableProperty UriProperty = BindableProperty.Create(propertyName: "Uri",
                returnType: typeof(string),
                declaringType: typeof(CustomWebView),
                defaultValue: default(string));

        public string Uri
        {
            get { return (string)GetValue(UriProperty); }
            set { SetValue(UriProperty, value); }
        }
    }


<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:PdfViewer;assembly=PdfViewer"
         x:Class="PdfViewer.MainPage"
         Padding="0,20,0,0">

<ContentPage.Content>
    <local:CustomWebView Uri="samplepdf.pdf" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
</ContentPage.Content>

[assembly: ExportRenderer(typeof(PdfViewer.CustomWebView), typeof(CustomWebViewRenderer))]
namespace PdfViewer.UWP
{
    public class CustomWebViewRenderer : WebViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                var customWebView = Element as CustomWebView;
                Control.Source = new Uri(string.Format("ms-appx-web:///Assets/pdfjs/web/viewer.html?file={0}", string.Format("ms-appx-web:///Assets/Content/{0}", WebUtility.UrlEncode(customWebView.Uri))));
            }
        }
    }
}

UWP - Assets - pdfjs structure

【问题讨论】:

  • 如果您没有提供任何代码,而只是提供其工作原理的链接,我们如何提供帮助?
  • public class CustomWebView : WebView { public static readonly BindableProperty UriProperty = BindableProperty.Create(propertyName: "Uri", returnType: typeof(string), declaringType: typeof(CustomWebView), defaultValue: default(string) );公共字符串 Uri { get { return (string)GetValue(UriProperty); } set { SetValue(UriProperty, value); } } }
  • 确保文件构建操作设置为内容。检查您指向文件的 Uri 是否正确。
  • 我已经将它更改为内容,但它仍然是空白的。顺便说一句,我正在尝试上传代码,但它要求我对其进行格式化,我认为它已经格式化。
  • 我已经上传了我的代码和一个图片文件来显示pdf.js和内容文件夹的文件夹结构

标签: xamarin.forms xamarin.uwp


【解决方案1】:

资产文件夹文件似乎发生了一些事情。当从 nuget 存储库更新时它起作用了。

【讨论】:

  • 感谢您的帮助。你又节省了我一天的时间来弄清楚哪里出了问题。
【解决方案2】:

当我按照官方文档实现HybridWebView 时遇到了类似的问题。

问题是PDF文件的build action属性不是Content。 请在您的 uwp 客户端项目中将构建操作设置为 Content

【讨论】:

  • 我已经将构建操作更改为内容。它仍然显示空白内容。文件的命名约定是否错误?我知道 Android 需要正确的命名约定。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-31
  • 1970-01-01
  • 1970-01-01
  • 2012-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多