【发布时间】:2018-05-29 18:00:34
【问题描述】:
我从我的服务器下载了一个 pdf 流。在我的应用程序中,我将字节数组以 pdf 格式保存到本地文件夹中。但是当我在 webview 中打开它时,它只显示一个白页。
我遵循了这个例子:https://developer.xamarin.com/recipes/cross-platform/xamarin-forms/controls/display-pdf。
这是我的 xaml 中的 customwebview:
<local:CustomWebView x:Name="customView" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
这是我的自定义视图代码:
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); }
}
}
这是我检索 pdf 字节数组并将其存储在本地的方法:
private async void ReadPDF()
{
var response = CommonLibrary.Helpers.HTTPClientHelper.DownloadPDF(AccountBL.AccessToken, APISettings.APIURI("api/booking/pdf"));
var streamContent = response.Content as System.Net.Http.StreamContent;
var bytes = CommonLibrary.Helpers.FileHelper.ReadBytes(await streamContent.ReadAsStreamAsync());
var dir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var fileName = "test.pdf";
CommonLibrary.Helpers.FileHelper.WriteFileFromByteArray(bytes, dir, fileName);
customView.Uri = System.IO.Path.Combine(dir, fileName);
}
我错过了什么吗?
【问题讨论】:
-
您是否尝试过让您的 webview 访问网站,只是为了确保是加载问题还是显示问题?
-
嗨,Jaxi,你是对的。一个正常的网站也没有加载。您只需设置 customWebview.Uri 我是否正确?
-
嗨 Jaxi,当我设置源属性时加载了一个普通站点 -> customView.Source = "google.nl"。当我将源设置为 pdf 位置时,它不起作用。
-
你能断点看看dir变量是否真的填充了目录吗?
-
我做了,它已被填充,我可以通过设备监视器找到该文件。
标签: c# xamarin.forms