1

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

Xamarin 表单 UWP - 显示 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 - 资产 - pdfjs 结构

4

2 回答 2

1

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

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

在此处输入图像描述

于 2017-06-23T02:29:48.863 回答
1

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

于 2017-06-23T18:56:42.363 回答