在 webview 控件中显示本地 PDF 文件 - 显示空白 Pdf 文件 [英] display local PDF file in webview control - Displays Blank Pdf File

查看:45
本文介绍了在 webview 控件中显示本地 PDF 文件 - 显示空白 Pdf 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on Xamarin Forms - UWP. I want to display local PDF file in webview control. I followed these 2 links :- https://developer.xamarin.com/recipes/cross-platform/xamarin-forms/controls/display-pdf/

Xamarin Forms UWP - 显示 PDF

它打开了pdf文件,但内容全是空白.任何人都可以帮我解决我可能遗漏的问题吗?

It opens the pdf file but content is all blank. Can anyone please help me in what I may be missing?

提前致谢!

这是我的代码:-自定义WebView.cs

Here is my code:- 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 结构

推荐答案

看起来 Asset 文件夹文件出了点​​问题.当从 nuget 存储库更新时,它起作用了.

Looks like something happened with Asset folder files. When was updated from nuget repository it worked.

这篇关于在 webview 控件中显示本地 PDF 文件 - 显示空白 Pdf 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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