【发布时间】:2017-11-26 02:50:15
【问题描述】:
我正在开发 Xamarin 表单 - UWP。 我想在 webview 控件中显示本地 PDF 文件。我关注了这两个链接:- https://developer.xamarin.com/recipes/cross-platform/xamarin-forms/controls/display-pdf/
Xamarin Forms UWP - Display 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))));
}
}
}
}
【问题讨论】:
-
如果您没有提供任何代码,而只是提供其工作原理的链接,我们如何提供帮助?
-
public class CustomWebView : WebView { public static readonly BindableProperty UriProperty = BindableProperty.Create(propertyName: "Uri", returnType: typeof(string), declaringType: typeof(CustomWebView), defaultValue: default(string) );公共字符串 Uri { get { return (string)GetValue(UriProperty); } set { SetValue(UriProperty, value); } } }
-
确保文件构建操作设置为内容。检查您指向文件的 Uri 是否正确。
-
我已经将它更改为内容,但它仍然是空白的。顺便说一句,我正在尝试上传代码,但它要求我对其进行格式化,我认为它已经格式化。
-
我已经上传了我的代码和一个图片文件来显示pdf.js和内容文件夹的文件夹结构