【发布时间】:2017-02-16 17:54:39
【问题描述】:
我目前正在使用 Windows 10 UWP 应用程序并面临 WebView 的问题,即当我的 HTML 内容较少时,我在 javascript 中的高度越来越高。我的代码如下
WebView webView = new WebView() { IsHitTestVisible = true };
string notifyJS = @"<script type='text/javascript' language='javascript'>
function setupBrowser(){
document.touchmove=function(){return false;};;
document.onmousemove=function(){return false;};
document.onselectstart=function(){return false;};
document.ondragstart=function(){return false;}
window.external.notify('ContentHeight:'+document.body.firstChild.offsetHeight);
//window.external.notify('ContentHeight:'+document.getElementById('pageWrapper').offsetHeight);
var links = document.getElementsByTagName('a');
for(var i=0;i<links.length;i++) {
links[i].onclick = function() {
window.external.notify(this.href);
return false;
}
}
}
</script>";
string htmlContent;
if (hexcolor != null)
{
htmlContent = string.Format("<html><head>{0}</head>" +
"<body onLoad=\"setupBrowser()\" style=\"margin:0px;padding:0px;background-color:{2};\">" +
"<div id=\"pageWrapper\" style=\"width:100%;word-wrap:break-word;padding:0px 25px 0px 25px\">{1}</div></body></html>",
notifyJS,
formItem.I_DEFAULT_VALUE,
hexcolor);
}
这里 formItem.I_DEFAULT_VALUE 是
HTML without html,head and body tags,其值为
<p>
<span style="font-size: 14px;">To help us investigate your query please can you make a sheet using the following </span><a href="http://www.pdf995.com/samples/pdf.pdf" style="font-size: 14px;" target="_blank">document</a><span style="font-size: 14px;">.</span></p>
<p>
<strong><span style="font-size: 14px;">Testing WebView Height</span></strong></p>
HexColor 是需要应用的背景颜色。
而我的script_notify方法如下:
webView.ScriptNotify += async (sender, e) =>
{
if (e.Value.StartsWith("ContentHeight"))
{
(sender as WebView).Height = Convert.ToDouble(e.Value.Split(':')[1]);
return;
}
if (!string.IsNullOrEmpty(e.Value))
{
string href = e.Value.ToLower();
if (href.StartsWith("mailto:"))
{
LauncherOptions options = new LauncherOptions();
options.DisplayApplicationPicker = true;
options.TreatAsUntrusted = true;
var success = await Launcher.LaunchUriAsync(new Uri(e.Value), options);
}
else if (href.StartsWith("tel:"))
{
LauncherOptions options = new LauncherOptions();
options.DisplayApplicationPicker = true;
options.TreatAsUntrusted = true;
var success = await Launcher.LaunchUriAsync(new Uri(e.Value), options);
}
else
{
LauncherOptions options = new LauncherOptions();
options.DisplayApplicationPicker = true;
options.TreatAsUntrusted = true;
var success = await Launcher.LaunchUriAsync(new Uri(e.Value), options);
}
}
};
有人可以建议为什么即使内容很小,我的高度也会变得很大?
【问题讨论】:
-
我用你的代码做了一个演示,但没有重现你的问题。 webView 的高度已正确调整大小。您能否发布
formItem.I_DEFAULT_VALUE的代码,或者分享一个可以重现此问题的基本演示。我正在使用WebView.NavigateToString来呈现 html 内容。 -
@ElvisXia-MSFT 我添加了
formItem.I_DEFAULT_VALUE,我也在使用WebView.NavigateToString。另外,我正在使用 C# 而不是 XAML 动态创建webview。 -
@ElvisXia-MSFT 也使用
window.external.notify('ContentHeight:'+document.body.firstChild.offsetHeight); -
仍然无法复制。你可以在这里试试我的演示:WebViewDynamicHeightSample.
-
:-( 不知道为什么它在我身边不起作用。我将尝试创建演示项目并检查是否可以重现该问题。
标签: javascript c# webview uwp windows-10-universal