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