在WP8中,控件WebBrowser没有提供对滚动条的支持,而在内置的IE中,却可以显示出滚动条(微软这是在坑我们吗),但如果在客户端使用Webbrowser的话,自己构造ScrollBar来显示

  通过javascript监听onscroll事件,然后传递给Webbrowser,通过onscroll的位置来确定自定义的ScrollBar的位置

下面演示方法:

1、在Html代码中添加javascript代码


function initialize() {
window.external.notify("scrollHeight=" + document.body.scrollHeight.toString());
window.external.notify("clientHeight=" + document.body.clientHeight.toString());
window.onscroll = onScroll;
}
function onScroll(e) {
var scrollPosition = document.body.scrollTop;
window.external.notify("scrollTop=" + scrollPosition.toString());
}
window.onload = initialize;

2、在xaml中定义控件

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<phone:WebBrowser x:Name="ContentWebBrowser" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"IsScriptEnabled="True" ScriptNotify="ContentWebBrowser_ScriptNotify"/>
<ScrollBar x:Name="DisplayScrollBar" Orientation="Vertical" HorizontalAlignment="Right" VerticalAlignment="Stretch" Minimum="0" Maximum="100" Value="0"/>
</Grid>

3、在Webbrowser添加 ScriptNotify 事件方法(注意:需要将Webbrowser的IsScriptEnabled属性设为True)


        private void ContentWebBrowser_ScriptNotify(object sender, NotifyEventArgs e)
{
var parts = e.Value.Split('=');
if (parts.Length != ) return; var number = ;
if (!int.TryParse(parts[], out number)) return; switch (parts[])
{
case "scrollHeight":
_scrollHeight = number;
if (_visibleHeight > )
{
DisplayScrollBar.Maximum = _scrollHeight - _visibleHeight;
}
break;
case "clientHeight":
_visibleHeight = number;
if (_scrollHeight > )
{
DisplayScrollBar.Maximum = _scrollHeight - _visibleHeight;
}
break; case "scrollTop":
DisplayScrollBar.Value = number;
break;
}
}

3、测试

  1)、定义一个WebBrowserHelper生成Html代码

    public class WebBrowserHelper
{
public static string NotifyScript
{
get
{
return @"<script>
window.onload = function(){
initialize();
}
function initialize() {
window.external.notify('scrollHeight=' + document.body.scrollHeight.toString());
window.external.notify('clientHeight=' + document.body.clientHeight.toString());
//window.onscroll = onScroll;
}
function onScroll(e) {
var scrollPosition = document.body.scrollTop;
window.external.notify('scrollTop=' + scrollPosition.toString());
}
</script>";
}
} public static string WrapHtml(string bodyContent, double viewportWidth)
{
var html = new StringBuilder();
html.Append("<html>");
html.Append(HtmlHeader(viewportWidth));
html.Append("<body>");
html.Append(bodyContent);
html.Append("</body>");
html.Append(NotifyScript);
html.Append("</html>");
return html.ToString();
} public static string HtmlHeader(double viewportWidth)
{
var head = new StringBuilder(); head.Append("<head>");
head.Append(string.Format("<meta name=\"viewport\" value=\"width={0}\" user-scalable=\"no\" />",
viewportWidth));
head.Append("</head>"); return head.ToString();
}
}

  2)传入HTML运行

var html = WebBrowserHelper.WrapHtml(
@"<div>123</div><div>123</div><div>123</div><div>123</div><div>123</div>
<div>123</div><div>123</div><div>123</div><div>123</div><div>123</div>
<div>123</div><div>123</div><div>123</div><div>123</div><div>123</div>
<div>123</div><div>123</div><div>123</div><div>123</div><div>123</div>
<div>123</div><div>123</div><div>123</div><div>123</div><div>123</div>
<div>123</div><div>123</div><div>123</div><div>123</div><div>123</div>
<div>123</div><div>123</div><div>123</div><div>123</div><div>123</div>
<div>123</div><div>123</div><div>123</div><div>123</div><div>123</div>
<div>123</div><div>123</div><div>123</div><div>123</div><div>123</div>
<div>123</div><div>123</div><div>123</div><div>123</div><div>123</div>
<div>123</div><div>123</div><div>123</div><div>123</div><div>123</div>
<div>123</div><div>123</div><div>123</div><div>123</div><div>123</div>
<div>123</div><div>123</div><div>123</div><div>123</div><div>123</div>", ContentWebBrowser.Width);
ContentWebBrowser.NavigateToString(html);

测试成功。

参考:http://www.mistergoodcat.com/post/Somethings-Missing-from-the-WebBrowser-Control

最新文章

  1. Android消息传递之Handler消息机制
  2. BZOJ4551——[Tjoi2016&amp;Heoi2016]树
  3. JQuery的ajaxFileUpload图片上传初试
  4. 关于未捕获异常的处理(WPF)
  5. java UDP 简单实现编程
  6. 第九章 企业项目开发--分布式缓存Redis(1)
  7. jQuery(Keep for myself)
  8. setuptools的使用
  9. ecshop改造读写分离配置与改造
  10. 【 POJ - 1204 Word Puzzles】(Trie+爆搜|AC自动机)
  11. (转)Python 遍历List三种方式
  12. 实现自己的MVC AJAX框架计划
  13. 解决svn图标不显示(绝对有用)
  14. 网络拓扑自动发掘之三层设备惯用的SNMP OID的含义
  15. bzoj2762-[JLOI2011]不等式组
  16. 工具SQL
  17. PHP的运行机制与原理(底层)
  18. kafka配置监控和消费者测试
  19. 转:初探nginx架构(二)
  20. Qt_2D_画图教程

热门文章

  1. PHP 常见header 状态
  2. spring batch中MyBatisPagingItemReader分页使用介绍
  3. JAVA-JSP内置对象之request获得所有的参数名称
  4. java基础篇---注解(Annotation)
  5. WPF中的数据驱动
  6. MFC使用自定义资源加载PNG
  7. hive和hbase区别和联系
  8. 1.nginx 防注入
  9. linq操作符:连接操作符
  10. USB2.0学习笔记连载(十四):USB驱动安装及固件程序的编写