【问题标题】:How to add White color to web view Background如何将白色添加到 Web 视图背景
【发布时间】:2019-10-16 00:47:53
【问题描述】:

我有一个网页视图来显示 pdf。当它显示 pdf 时,它没有占据整个区域,这不是我的问题,但它的背景颜色显示了一种深色背景,我需要将深色移除为白色如何在 xamarin 表单中执行此操作

【问题讨论】:

    标签: c# xaml xamarin.forms webview


    【解决方案1】:

    您是否使用 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有几页,它可以滚动:

    【讨论】:

    • customWebView.Uri 应该是什么格式它不预览我的项目它说没有可用的预览
    • 是的,它解决了颜色问题,但它无法滚动,并且它有一些我不想要的字段,因为我有一个自定义按钮,它会在它接触 webview 后显示。可以覆盖这些问题
    • @C_Subash 它在我这边滚动得很好,请看我的截图。你不想要哪个部分?
    • 滚动后有一个黑框,页码和放大/缩小我想删除它
    • @C_Subash 不幸的是,这是一个 Google 文档视图,我们无法根据需要对其进行自定义。我们必须接受带有嵌入属性的样式为真或假。
    猜你喜欢
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多