Android中的Xamarin Forms WebView:无法在WebView中打开HTML中的PDF链接

时间:2019-02-27 19:31:16

标签: android xamarin.forms webview

我正在将来自Web服务器的html页面加载到Xamarin Forms WebView中。此html页面包含指向PDF文件的链接。

在iOS中,当用户单击链接时,将下载并打开PDF文件。 在Android中,当用户单击链接时,没有任何反应。请注意,Android设备上已安装了多个PDF Viewer。

是否需要进行任何配置才能使其在Android中工作?

代码如下:

if (dict.ContainsKey("url")){
    string url = dict["url"]; 
    if (Uri.IsWellFormedUriString(url, UriKind.Absolute)){
        NotifWebView.Source = url;
    } else {
        var htmlSource = new HtmlWebViewSource();
        htmlSource.Html = @"<html><body><h1>Fehler!</h1><p>Ungültige oder keine URL oder ungültiges HTML.</p></body></html>";
        NotifWebView.Source = htmlSource;
    }
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您可以使用 https://github.com/xamarin/recipes/tree/master/Recipes/xamarin-forms/Controls/display-pdf

由于缺乏平台支持,WebView控件可以在iOS平台上显示PDF文件,但不能在Android和Windows Phone平台上显示PDF文件。您需要创建一个自定义渲染器才能在android平台上实现它。如下:

  1. 创建Xamarin.Forms自定义控件
  2. 使用Xamarin.Forms中的自定义控件。
  3. 在每个平台上为控件创建自定义渲染器。

如果您愿意花一些钱,看起来Syncfusion提供了一种用于查看和编辑PDF文件的解决方案。 http://www.syncfusion.com/products/xamarin

相关问题