【发布时间】:2019-10-16 00:47:53
【问题描述】:
【问题讨论】:
标签: c# xaml xamarin.forms webview
【问题讨论】:
标签: c# xaml xamarin.forms webview
您是否使用 Google 文档查看器来加载远程 pdf 文件?如果是这样,则无法删除额外的边框,因为它由 Google 控制。我没有找到任何 api 来改变这种外观。但是你可以改变嵌入的样式:
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("https://drive.google.com/viewerng/viewer?embedded=true&url=" + customWebView.Uri);
}
}
当我们将embedded设置为true时,它会显示一个浅灰色的边框。
您必须在 Forms 上创建一个自定义控件才能使用上面的自定义渲染器:
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); }
}
}
最后,在 XAML 中使用它:
<StackLayout>
<local:CustomWebView VerticalOptions="FillAndExpand" Uri="http://www.africau.edu/images/default/sample.pdf" />
</StackLayout>
更新:
如果pdf有几页,它可以滚动:
【讨论】: