【问题标题】:Adding background color to Xamarin Forms WebView has no impact向 Xamarin Forms WebView 添加背景颜色没有影响
【发布时间】:2020-01-22 13:53:57
【问题描述】:

页面标记

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage NavigationPage.HasNavigationBar="false" xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ConnectedLifeView.NativeApp.Pages.LoginSuccessPage">
    <ContentPage.Content>
        <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Margin="0" Padding="0">
            <WebView x:Name="webView" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" BackgroundColor="#02114D"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

输出

我尝试将背景颜色添加到内容页面并将 WebView 背景颜色设置为透明,但它也没有效果。

我的所有其他页面都有正确的背景,但是这个页面看起来有点奇怪,因为它在 WebView 中加载 URL 时显示白色背景。

Xamarin.Forms 版本:4.2.0.815419

平台:安卓

【问题讨论】:

  • 加载具有适当背景颜色的静态 HTML 字符串
  • 添加静态 HTML 不起作用,因为 WebView 在尝试加载 URL 后立即变为空白

标签: android xaml xamarin xamarin.forms xamarin.forms.webview


【解决方案1】:

如果你想改变 webview 背景颜色,另一种方法是使用自定义渲染。

public class TransparentWebViewRenderer:WebViewRenderer

{
    protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
    {
        base.OnElementChanged(e);

        // Setting the background as transparent
        this.Control.SetBackgroundColor(Android.Graphics.Color.AliceBlue);
    }

}

【讨论】:

  • 我能够通过使用自定义渲染器来达到预期的效果,但我仍然想知道如果没有效果,在元素上设置 BackgroundColor 有什么用?
  • @Vikram,我猜是设计问题,你可以在 github 上创建讨论。如果我的回复解决了您的问题,请记得将我的回复标记为 answer.thanks。
  • 您的回答部分解决了我的问题。我观察到 Xamarin Forms WebView 的其他问题,此链接对我的帮助最大 lukealderton.com/blog/posts/2016/may/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-19
  • 2019-02-09
相关资源
最近更新 更多