惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LINUX DO - 最新话题
G
Google Developers Blog
J
Java Code Geeks
The GitHub Blog
The GitHub Blog
F
Full Disclosure
H
Help Net Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Vercel News
Vercel News
酷 壳 – CoolShell
酷 壳 – CoolShell
Recent Announcements
Recent Announcements
Help Net Security
Help Net Security
The Hacker News
The Hacker News
IT之家
IT之家
Y
Y Combinator Blog
Martin Fowler
Martin Fowler
L
Lohrmann on Cybersecurity
C
CERT Recently Published Vulnerability Notes
V
Visual Studio Blog
博客园 - 聂微东
Hacker News: Ask HN
Hacker News: Ask HN
H
Hacker News: Front Page
Know Your Adversary
Know Your Adversary
Security Latest
Security Latest
Security Archives - TechRepublic
Security Archives - TechRepublic
Simon Willison's Weblog
Simon Willison's Weblog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Last Week in AI
Last Week in AI
Schneier on Security
Schneier on Security
N
News and Events Feed by Topic
博客园 - 【当耐特】
有赞技术团队
有赞技术团队
AWS News Blog
AWS News Blog
Blog — PlanetScale
Blog — PlanetScale
博客园_首页
Google DeepMind News
Google DeepMind News
Cloudbric
Cloudbric
N
News | PayPal Newsroom
A
About on SuperTechFans
S
Schneier on Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Hugging Face - Blog
Hugging Face - Blog
M
MIT News - Artificial intelligence
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
雷峰网
雷峰网
T
The Exploit Database - CXSecurity.com
罗磊的独立博客
K
Kaspersky official blog
The Cloudflare Blog
I
Intezer

博客园 - Windie Chai

为SharePoint顶部链接开发自定义数据源 SharePoint 2010 JavaScript技巧两则 Windows Phone Background Agent杂谈 Linq to SharePoint,看上去很美 Windows Phone自定义主题 《Visual Studio程序员箴言》笔记 HTML5学习碎片 Windows Phone 7 UI设计和交互规范随笔(2) Windows Phone 7 UI设计和交互规范随笔(1) 使用SharePoint Client OM来查询列表的注意事项(2) 使用SharePoint Client OM来查询列表的注意事项 小心Windows Live Writer插件偷走你的博客密码 [C#]增强响应性,用加载窗体(Splash)来载入主窗体 WindStyle ExifInfo for Windows Live Writer发布 WF4.0活动模型(1):工作流既活动 泛谈SharePoint 2010无代码工作流 如何在SharePoint 2010项目中引用UserProfiles.dll Windstyle SlugHelper for Windows Live Writer发布 让代码看起来更舒服(2):选择适合的字体
Windows Phone WebBrowser的技巧
Windie Chai · 2012-03-07 · via 博客园 - Windie Chai

2012-03-07 17:23  Windie Chai  阅读(3021)  评论()    收藏  举报

无论是在桌面级开发中,还是在手机端开发中,WebBrowser都是一个经常会用到的控件;Windows Phone中的WebBrowser虽然远远没有桌面版那么强大,但依然足够应付常规用途。本文就来介绍几则Windows Phone中的WebBrowser控件的小技巧。 

1.显示HTML片段

WebBrowser的NavigateToString方法可以用来将一段HTML片段显示在WebBrowser中。利用这个方法可以把WebBrowser当作一个增强版的RichTextBox来使用,京东商城的Windows Phone客户端在展示商品信息时就使用了这种技术。而且这种技术还有助于解决Windows Phone中TextBlock显示长文本的一个bug,具体表现为当文本过长时,TextBlock只显示文本的前半段内容,后半段内容不予显示,但却留出了位置(滚动条还能到达,非常诡异),而WebBrowser在显示长文本时就没有这种困扰。

但NavigateToString并不是完美的,假若传入的字符串中包含中文(或其他UTF-8字符)的话,就会显示为乱码。

解决这个问题的方法之一是提前对字符串进行转码,可以参考这篇文章。但这样做的代价是需要遍历所有字符,其实只要把需要显示的HTML片段简单构造成HTML文件,存储到独立存储中,然后再用WebBrowser以常规的方式打开即可解决这个问题。如下面的代码所示:

using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!file.DirectoryExists("temp"))
file.CreateDirectory("temp");
using (IsolatedStorageFileStream fs = new IsolatedStorageFileStream("temp\\review.html", FileMode.Create, file))
{
string html = "<!DOCTYPE html><html lang='zh-CN'><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'></head><body>";
html += e.Review.Summary;
html += "</body></html>";
byte[] bytes = Encoding.UTF8.GetBytes(html);
fs.Write(bytes, 0, bytes.Length);
}
}
this.wb.Navigate(new Uri("temp\\review.html", UriKind.Relative));

2.禁止缩放

WebBrowser支持缩放,但有时我们并不需要缩放功能,譬如在用它来解决TextBlock的长文本bug时。

如果需要禁用一切手势,可以将WebBrowser的IsHitTestVisible设置为False,但这样做的后果是WebBrowser滑动显示内容的功能都会失去。但如果仅仅想要禁用缩放功能,可以在WebBrowser将要显示的HTML的Head中加入这样下面的代码:

<meta name='viewport' content='width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0' />

关于viewport的更多细节可以参考MSDN Blog的这篇文章。值得注意的是原文在发表时,Windows Phone中的IE还不支持initial-scale、minimum-scale和maximum-scale,而在最新的Windows Phone Mango更新中,除了initial-scale之外的其余属性都已经可以很好的支持了。

3.接管横向滑动

如果用WebBrowser来解决TextBlock的bug,那么还有一个问题需要注意,WebBrowser会接管横向滑动手势,用来移动页面位置,也就是说,假如你把WebBrowser方知道Pivot或Panorama控件中时,就没有办法切换到其他Item了(除非从Header部分横向滑动)。

不过一般在用WebBrowser代替TextBlock时,并需要它内置的横向滑动功能(通常会禁用缩放),所以我们可以想办法侦测发生在WebBrowser上的横向滑动手势,并用来修改Pivot或Panorama的SelectedIndex。所幸的是,Silverlight Toolkit For Windows Phone中有一个组件可以帮我们轻松的完成这项工作。

首先在Xaml中添加GestureService.GestureListener:

<phone:WebBrowser Name="wb" Loaded="wb_Loaded" LoadCompleted="wb_LoadCompleted">
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener Flick="GestureListener_Flick" />
</toolkit:GestureService.GestureListener>
</phone:WebBrowser>

在GestureService.GestureListener的Flick事件中修改Pivot或Panorama的SelectedIndex:

private void GestureListener_Flick(object sender, FlickGestureEventArgs e)
{
if (e.Direction.ToString() == "Horizontal")
{
this.p.SelectedIndex = 1;
}
}

除了Flick之外,GestureService还支持Tap、Double Tap、Touch and Hold、Pan、以及Pinch and Stretch多种手势。关于GestureService的更多信息请参考这篇文章