在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的更多相关文章

  1. 【WP8】WebBrowser相关

    2014年09月02日更新 今天用了一下WebBrowser,在使用过程中也遇到了一些问题,在这里做一下记录 虽然WebBrowser比较重,会比较影响性能(除非一定要用到它,否则尽量少用),但有时候 ...

  2. 【WP8.1】WebView笔记

    之前在WP8的时候做过WebBrowser相关的笔记,在WP8.1的WebView和WebBrowser有些不一样,在这里做一些笔记 下面分为几个部分 1.禁止缩放 2.JS通知后台C#代码(noti ...

  3. UGUI之Scrollbar使用

    这个效果主要用到了3个组件(对象): 1:Scrollbar对象  滚动条 2:Scroll Rect组件  让对象具有滑动效果 3:Mask组件  遮罩层.把多余的部分隐藏不显示 Scrollbar ...

  4. WPF 使用winform的webbrowser

    首先要添加如下引用: WindowsFormsIntegration System.Drawing System.Windows.Forms 然后在xaml中添加引用 xmlns:winform=&q ...

  5. Unity实现滑页效果(UGUI)

    简介 项目需要...直接展示效果吧: 原理 使用UGUI提供的ScrollRect和ScrollBar组件实现基本滑动以及自己控制每次移动一页来达到滑页的效果. 实现过程 1.创建两个panel,上面 ...

  6. uGUI练习(七) Drag And Drop

    练习目标 练习UI的拖放操作 一.相关组件 EventTrigger Canvas Group ScrollRect Mask Scrollbar 二.拖放练习 1.创建一个Panel,命名Panel ...

  7. uGUI练习(六) ScrollView

    练习目标 练习uGUI的滑动组件 一.相关组件 ScrollRect Mask Grid Layout Group Scrollbar 二.步骤 1.创建一个Panel,命名为ScrollRect,添 ...

  8. Windows phone 8 学习笔记(2) 数据文件操作(转)

    Windows phone 8 应用用于数据文件存储访问的位置仅仅限于安装文件夹.本地文件夹(独立存储空间).媒体库和SD卡四个地方.本节主要讲解它们的用法以及相关限制性.另外包括本地数据库的使用方式 ...

  9. Windows phone 8 学习笔记(2) 数据文件操作

    原文:Windows phone 8 学习笔记(2) 数据文件操作 Windows phone 8 应用用于数据文件存储访问的位置仅仅限于安装文件夹.本地文件夹(独立存储空间).媒体库和SD卡四个地方 ...

随机推荐

  1. java基础解疑!!

    解疑1:抽象关键字为什么不能和 private. static. final 共存 ①private 是私有的意思,当它修饰方法的时候子类是不能够继承父类私有方法的,但是 abstract 修饰的方法 ...

  2. [转]详解Oracle高级分组函数(ROLLUP, CUBE, GROUPING SETS)

    原文地址:http://blog.csdn.net/u014558001/article/details/42387929 本文主要讲解 ROLLUP, CUBE, GROUPING SETS的主要用 ...

  3. 【WPF】修改数据层ViewModel后,UI界面未同步更新

    界面:WPF(MVVM)中将集合类控件ItemsControl的ItemsSource绑定到了ViewModel中的ObservableCollection列表,ItemsControl.ItemTe ...

  4. html标签之img

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. python list数据写入文件

    看代码 def writeLmk(self,fileName,landmarks): fp = open(fileName,'w+') fp.write( "version: 1" ...

  6. Java编程的逻辑 (38) - 剖析ArrayList

    本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...

  7. IIS 7上部署PHP【后续一】

    在之前成功在windows 2008+iis7环境下部署php的基础上,今天把之前的挂Q网站转移到了这个服务器下. 文件拷贝到服务器后,问题继续出现. 首先出现的问题是,Mysql的数据库名称和账户密 ...

  8. ansible普通用户su切换

    [root@361way.com ~]# cat /etc/ansible/hosts [test01] 10.212.52.14 ansible_ssh_user=test ansible_ssh_ ...

  9. 【html】关于锚点的一些事

    今天修改公会系统,有用到锚点对页面位置进行控制,结果碰到了一些问题,通过查询相关资料解决了,在这里总结下. 两种方法跳转到锚点: 1.给锚点添加 name 属性和 id 属性.一般只要加 name 就 ...

  10. Python 内置方法new

    class Dog(object): def __new__(self): print("i am new .") def __init__(self): print(" ...