我正在尝试在本地xml文件windows phone web浏览器控件中显示可用的html内容。我使用下面的代码向web broser显示内容,
StringBuilder sb = new StringBuilder();
sb.Append(@"<html><head>");
sb.Append(@"<meta name=""viewport"" content=""width=""device-width"">");
sb.Append(@"<style type=""text/css"">");
sb.Append(@"body {");
sb.Append(@" margin:5px;");
sb.Append(@" text-align:center;");
sb.Append(@" letter-spacing:0.1em;");
sb.Append(@" font-size-adjust: none;");
sb.Append(@" font-size: 14px;");
sb.Append(@" font-family:""Segoe WP"";");
sb.Append(@" }");
sb.Append("p");
sb.Append("{margin:5px;}");
sb.Append(@"</style></head><body>");
sb.Append(commentry);
sb.Append(@"</body></html>");
discusswebBrowser.NavigateToString(sb.ToString());
内容将在Web浏览器中显示,但最后几行正在修剪/未显示。我已经尝试过更改参数,控件的高度等,但是不管内容长度如何,仍然没有显示很少的结束行。我甚至尝试在网络浏览器中放入纯文本。 我使用Web浏览器控件的原因是因为内容是为html页面格式化的,并且还提供了捏缩放功能。 控制定义如下:
<Grid x:Name="ContentPanel" Grid.Row="1" >
<ScrollViewer Grid.Row="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<phone:WebBrowser Name="discusswebBrowser" Height="1000" />
</ScrollViewer>
</Grid>
答案 0 :(得分:1)
无法找到这个问题的任何答案,所以提出了我自己的解决方法。首先,我检查要显示的内容的长度,然后如果超过4000个字符长度,那么我将它分成两部分。然后还添加一个带有更多按钮的应用程序栏,以便用户可以单击该按钮并查看剩余的文本。这是代码
{
if (commentry.Length > 4000 && commentry.IndexOf("<p>", commentry.Length / 2) > 0)
{
//Change the xaml to contain a panaroma , and if then on the second item create web browser control with the second half of the commentry.
commentry1 = commentry.Remove(commentry.IndexOf("<p>", commentry.Length / 2), commentry.Length - commentry.IndexOf("<p>", commentry.Length / 2));
commentry2 = commentry.Remove(0, commentry.IndexOf("<p>", commentry.Length / 2));
appBarMoreButton = new ApplicationBarIconButton(new Uri("/Images/quote.back.png", UriKind.Relative));
appBarMoreButton.Text = "more";
appBarMoreButton.Click += new EventHandler(loadMoreContent);
appBarPreviousButton = new ApplicationBarIconButton(new Uri("/Images/quote.back.png", UriKind.Relative));
appBarPreviousButton.Text = "Previous";
appBarPreviousButton.Click += new EventHandler(loadFirstPart);
}
var htmlScript = "<script>function getDocHeight() { " +
"return document.getElementById('pageWrapper').offsetHeight;" +
"}" +
"function SendDataToPhoneApp() {" +
"window.external.Notify('' + getDocHeight());" +
"}</script>";
if (commentry1 == null && commentry2 == null)
{
var htmlConcat = string.Format("<html><meta name=\"viewport\" content=\"width=device-width,user-scalable=yes,height=device-height\" /><head>{0}</head>" +
"<body style=\"margin:5px;padding:0px;background-color:{3};\" " +
"onLoad=\"SendDataToPhoneApp()\">" +
"<div id=\"pageWrapper\" style=\"width:100%;color:{2}; background-color:{3}\"> " +
"{1}</div></body><footer></footer></html>",
htmlScript,
commentry, fontColor, backGroundColor);
discusswebBrowser.NavigateToString(htmlConcat);
}
else
{
var htmlConcat = string.Format("<html><meta name=\"viewport\" content=\"width=device-width,user-scalable=yes,height=device-height\" /><head>{0}</head>" +
"<body style=\"margin:5px;padding:0px;background-color:{3};\" " +
"onLoad=\"SendDataToPhoneApp()\">" +
"<div id=\"pageWrapper\" style=\"width:100%;color:{2}; background-color:{3}\"> " +
"{1}</div></body><footer></footer></html>",
htmlScript,
commentry1, fontColor, backGroundColor);
discusswebBrowser.NavigateToString(htmlConcat);
ApplicationBar = new ApplicationBar();
ApplicationBar.IsVisible = true;
ApplicationBar.Mode = ApplicationBarMode.Minimized;
ApplicationBar.IsMenuEnabled = false;
ApplicationBar.Buttons.Add(appBarMoreButton);
}
discusswebBrowser.ScriptNotify +=
new EventHandler<NotifyEventArgs>(wb1_ScriptNotify);
}
对于more按钮的事件处理程序,创建一个函数,该函数将加载剩余文本以及应用程序栏中的按钮以移回第一部分。
private void loadMoreContent(object sender, EventArgs e)
{
if (commentry2 != null)
{
var htmlScript = "<script>function getDocHeight() { " +
"return document.getElementById('pageWrapper').offsetHeight;" +
"}" +
"function SendDataToPhoneApp() {" +
"window.external.Notify('' + getDocHeight());" +
"}</script>";
var htmlConcat = string.Format("<html><meta name=\"viewport\" content=\"width=device-width,user-scalable=yes,height=device-height\" /><head>{0}</head>" +
"<body style=\"margin:5px;padding:0px;background-color:{3};\" " +
"onLoad=\"SendDataToPhoneApp()\">" +
"<div id=\"pageWrapper\" style=\"width:100%;color:{2}; background-color:{3}\"> " +
"{1}</div></body><footer></footer></html>",
htmlScript,
commentry2, fontColor, backGroundColor);
discusswebBrowser.NavigateToString(htmlConcat);
this.ApplicationBar.Buttons.RemoveAt(0);
this.ApplicationBar.Buttons.Add(appBarPreviousButton);
}
}
答案 1 :(得分:0)
您需要删除硬编码的Height="1000"
,而ScrollViewer
中的Grid.Row =“1”是不必要的,因为您没有为ContentPanel定义行定义
<Grid x:Name="ContentPanel" Grid.Row="1" >
<ScrollViewer VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<phone:WebBrowser Name="discusswebBrowser" />
</ScrollViewer>
</Grid>
此外,您不需要ScrollViewer作为电话:WebBrowser内部具有滚动机制。
应该有效的最终片段
<Grid x:Name="ContentPanel" Grid.Row="1" >
<phone:WebBrowser Name="discusswebBrowser" />
</Grid>