在我的Xamarin Forms应用程序中,我使用了一个WebView作为表单,通过插入一个带有contentEditable设置为true的div。这一切都很好,直到更新到AppCompat样式后出现了奇怪的白色遮罩层(白色正方形),当用户尝试在WebView中写入时,该遮罩层会消失。为了找到问题的根本原因,我创建了一个简单的项目,只包含一个页面,其中包含一个垂直堆栈布局,包含另一个堆栈布局和WebView。我注意到,如果内部堆栈布局的高度足以覆盖WebView,当键盘出现时,就会出现此问题。在这种情况下,WebView被推上去,出现了这个奇怪的遮罩层。WebView是完全功能的,可以编写文本,即使它被这个白色正方形所隐藏,当键盘被解除时,该白色正方形会消失。这是一个简单页面的例子,可用于测试该问题。按钮仅添加了增加和减小布局大小以更容易地显示问题。
public class MainPage : ContentPage
{
const string ContentEdit = @"<div contentEditable=""true"" style =""min-height: 200px"">";
const string ContentEditEnd = "</div>";
const string EmptyDocument = @"<body>" + ContentEdit + ContentEditEnd + @"</body>";
WebView webView;
public MainPage()
{
InitializePage();
}
void InitializePage()
{
webView = new WebView()
{
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
};
var button1 = new Button
{
HorizontalOptions = LayoutOptions.CenterAndExpand,
VerticalOptions = LayoutOptions.CenterAndExpand,
Text = "-",
};
var button2 = new Button
{
HorizontalOptions = LayoutOptions.CenterAndExpand,
VerticalOptions = LayoutOptions.CenterAndExpand,
Text = "+",
};
var tallLayout = new StackLayout()
{
Orientation = StackOrientation.Horizontal,
HeightRequest = 200,
BackgroundColor = Color.Lime,
Children = { button1, button2, },
};
button1.Clicked += (sender, e) => tallLayout.HeightRequest = tallLayout.Height - 20;
button2.Clicked += (sender, e) => tallLayout.HeightRequest = tallLayout.Height + 20;
var layout = new StackLayout
{
Orientation = StackOrientation.Vertical,
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
Children = { tallLayout, webView },
};
Content = layout;
}
protected override void OnAppearing()
{
InitializeEmptyContent();
}
public void InitializeEmptyContent()
{
var htmlSource = new HtmlWebViewSource();
htmlSource.Html = EmptyDocument;
webView.Source = htmlSource;
}
}
我尝试使用本机Android也复现了这个简单的布局,但似乎没有发现类似的错误。这是Xamarin Forms的bug还是我的操作有误?