如何在自定义webview xamarin表单中显示pdf

如何在自定义webview xamarin表单中显示pdf,xamarin,xamarin.forms,xamarin.android,Xamarin,Xamarin.forms,Xamarin.android,我想使用pdfjs从url在webview中显示pdf,但我得到的是空视图。这是我的密码 CustomWebView.cs public class CustomWebView : WebView { public static readonly BindableProperty UriProperty = BindableProperty.Create(propertyName: "Uri", returnTyp

我想使用pdfjs从url在webview中显示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); }
            }
        }
MainPage.xaml

 <ContentPage.Content>
        <local:CustomWebView Uri="http://veezo2007pkk.somee.com/api/DiagnosticDetail/RetrieveFile/1" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
    </ContentPage.Content>

安卓应用程序

CustomWebViewRenderer.cs

 public class CustomWebViewRenderer : WebViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                var customWebView = Element as CustomWebView;
                Control.Settings.AllowUniversalAccessFromFileURLs = true;
                Control.LoadUrl(string.Format("file:///android_asset/pdfjs/web/viewer.html?file={0}", string.Format("file:///android_asset/Content/{0}", WebUtility.UrlEncode(customWebView.Uri))));
            }
        }
    }
公共类CustomWebViewRenderer:WebViewRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(例如NewElement!=null)
{
var customWebView=作为customWebView的元素;
Control.Settings.allowuniversalaccessfromfileurl=true;
Control.LoadUrl(string.Format(“file:///android_asset/pdfjs/web/viewer.html?file={0}”,string.Format(“file:///android_asset/Content/{0},WebUtility.UrlEncode(customWebView.Uri));
}
}
}

它看起来不错,但我不知道为什么它没有显示在webview中……

这里我假设两件事。首先,你对在
WebView
中显示的PDF文件不感兴趣。其次,您可以使用第三方库


我建议使用。它非常容易使用,可以做任何你想用PDF做的事情——可能不是所有,但很多。如果您担心需要为这些控件支付大量费用,请不要担心,您可以使用它(如果您的收入不超过100万美元,并且您的团队规模不超过5个开发人员)。您可能想看一看,谈论从URL显示PDF。

您的移动内部存储或某些web链接中是否有此PDF?您是否有任何代码示例,如何在syncfusion PDF viewer中显示二进制数据?因为我的数据库中有二进制格式的pdf数据,所以请看一下PdfViewer入门(我回答中的第一个链接)。基本上,您需要将数据转换为流,并将其绑定到
SfPdfViewer
上的
InputFileStream
属性。这个问题不是免费的,正如我在回答中所说,有一个社区版本-是免费的。但是,您确实需要满足我的答案中列出的两个条件。