【WP8】为Webbrowser添加ScrollBar
在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
【WP8】为Webbrowser添加ScrollBar的更多相关文章
- 【WP8】WebBrowser相关
2014年09月02日更新 今天用了一下WebBrowser,在使用过程中也遇到了一些问题,在这里做一下记录 虽然WebBrowser比较重,会比较影响性能(除非一定要用到它,否则尽量少用),但有时候 ...
- 【WP8.1】WebView笔记
之前在WP8的时候做过WebBrowser相关的笔记,在WP8.1的WebView和WebBrowser有些不一样,在这里做一些笔记 下面分为几个部分 1.禁止缩放 2.JS通知后台C#代码(noti ...
- UGUI之Scrollbar使用
这个效果主要用到了3个组件(对象): 1:Scrollbar对象 滚动条 2:Scroll Rect组件 让对象具有滑动效果 3:Mask组件 遮罩层.把多余的部分隐藏不显示 Scrollbar ...
- WPF 使用winform的webbrowser
首先要添加如下引用: WindowsFormsIntegration System.Drawing System.Windows.Forms 然后在xaml中添加引用 xmlns:winform=&q ...
- Unity实现滑页效果(UGUI)
简介 项目需要...直接展示效果吧: 原理 使用UGUI提供的ScrollRect和ScrollBar组件实现基本滑动以及自己控制每次移动一页来达到滑页的效果. 实现过程 1.创建两个panel,上面 ...
- uGUI练习(七) Drag And Drop
练习目标 练习UI的拖放操作 一.相关组件 EventTrigger Canvas Group ScrollRect Mask Scrollbar 二.拖放练习 1.创建一个Panel,命名Panel ...
- uGUI练习(六) ScrollView
练习目标 练习uGUI的滑动组件 一.相关组件 ScrollRect Mask Grid Layout Group Scrollbar 二.步骤 1.创建一个Panel,命名为ScrollRect,添 ...
- Windows phone 8 学习笔记(2) 数据文件操作(转)
Windows phone 8 应用用于数据文件存储访问的位置仅仅限于安装文件夹.本地文件夹(独立存储空间).媒体库和SD卡四个地方.本节主要讲解它们的用法以及相关限制性.另外包括本地数据库的使用方式 ...
- Windows phone 8 学习笔记(2) 数据文件操作
原文:Windows phone 8 学习笔记(2) 数据文件操作 Windows phone 8 应用用于数据文件存储访问的位置仅仅限于安装文件夹.本地文件夹(独立存储空间).媒体库和SD卡四个地方 ...
随机推荐
- django 部署,gunicorn、virtualenv、nginx
声明: 1.本篇文章是我边写命令边写的,请尊重我的劳动成果,转载请加上链接. 2.我既然公开写出来,是希望大家遇到问题的时候有个参考,所以,大家可以免费转载,使用该文章 3.但是,如果你要用这篇文章来 ...
- 一个JS引发的血案
转载一篇大师傅的文章: 原文链接:http://xn--i2r.ml/index.php/2017/08/05/39.html 又到了周末,闲来无聊,挖挖补天 找了个目标,发现一个站 查看源码发现一个 ...
- Maven 解决 下载项目 compiler 为1.5的问题
在 开发Maven 项目的时候,会发现个问题,就是下载下来的项目默认 compiler 为1.5 ,项目报错. 明明之前开发用的是1.7的啊. 这里只需要在pom.xml确定下就好了. <pro ...
- 用B表更新A表
UPDATE dbo.POR_LOT SET POR_LOT.SHIPPED_FLAG =B.SHIPPED_FLAGFROM POR_LOT A INNER JOIN ( SELECT T5.LOT ...
- DLL接口的实现(虚函数)
DLL接口的实现(虚函数) 我们在c++编程过程中往往要用到各种不同形式的程序库,这些库的发布方式有动态库和静态库.对于静态类库,设计良好的静态类库能实现功能上的隔离,无法避免类库实现必须重新编译.链 ...
- 百度网盘 http://pandownload.com/index.html
https://github.com/high-speed-downloader/high-speed-downloader
- 【Java集合源码剖析】HashMap源码剖析
转载出处:http://blog.csdn.net/ns_code/article/details/36034955 HashMap简介 HashMap是基于哈希表实现的,每一个元素是一个key-va ...
- springmvc搭建环境时报No mapping found for HTTP request with URI [/exam3/welcome] in DispatcherServlet with name 'spring2'
项目是使用spring MVC (1)在浏览器中访问,后台总报错: No mapping found for HTTP request with URI [/exam3/welcome] in Dis ...
- 通过 Spark R 操作 Hive
作为数据工程师,我日常用的主力语言是R,HiveQL,Java与Scala.R是非常适合做数据清洗的脚本语言,并且有非常好用的服务端IDE——RStudio Server:而用户日志主要储存在hive ...
- 总结一些笔记上的C和C++知识点
1.如果没有提供自己的copy构造函数,C++会提供一个默认的copy构造函数,是成员到成员的copy. 2.C中static的作用: 1.隐藏:如果未加static,编译多个文件的时候,所有的全局变 ...