概述

Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, Ironpython,对JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。《一步一步学Silverlight 2系列》文章将从Silverlight 2基础知识、数据与通信、自定义控件、动画、图形图像等几个方面带您快速进入Silverlight 2开发。

本文是Silverlight 2与浏览器交互的最后一篇,将介绍相关的辅助类方法。

获取浏览器信息

在Silverlight 2中提供了获取浏览器信息的一个类BrowserInformation,可供我们直接调用,如获取浏览器名称及浏览器版本,是否禁用Cookies等信息。做一个简单的示例,定义XAML如下:

<Grid x:Name="LayoutRoot" Background="#CDFCAE">
<Grid.RowDefinitions>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="140"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300"></ColumnDefinition>
<ColumnDefinition Width="300"></ColumnDefinition>
</Grid.ColumnDefinitions> <TextBlock Text="Name:" Style="{StaticResource title}"
Grid.Row="0" Grid.Column="0"></TextBlock>
<TextBlock x:Name="Name" Style="{StaticResource content}"
Grid.Row="0" Grid.Column="1"></TextBlock> <TextBlock Text="BrowserVersion:" Style="{StaticResource title}"
Grid.Row="1" Grid.Column="0"></TextBlock>
<TextBlock x:Name="BrowserVersion" Style="{StaticResource content}"
Grid.Row="1" Grid.Column="1"></TextBlock> <TextBlock Text="CookiesEnabled:" Style="{StaticResource title}"
Grid.Row="2" Grid.Column="0"></TextBlock>
<TextBlock x:Name="CookiesEnabled" Style="{StaticResource content}"
Grid.Row="2" Grid.Column="1"></TextBlock> <TextBlock Text="Platform:" Style="{StaticResource title}"
Grid.Row="3" Grid.Column="0"></TextBlock>
<TextBlock x:Name="Platform" Style="{StaticResource content}"
Grid.Row="3" Grid.Column="1"></TextBlock> <TextBlock Text="UserAgent:" Style="{StaticResource title}"
Grid.Row="4" Grid.Column="0"></TextBlock>
<TextBlock x:Name="UserAgent" Style="{StaticResource content}"
Grid.Row="4" Grid.Column="1" TextWrapping="Wrap"></TextBlock>
</Grid>

在Loaded事件中获取相关信息:

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
BrowserInformation browser = HtmlPage.BrowserInformation; Name.Text = browser.Name;
BrowserVersion.Text = browser.BrowserVersion.ToString();
CookiesEnabled.Text = browser.CookiesEnabled.ToString();
Platform.Text = browser.Platform;
UserAgent.Text = browser.UserAgent;
}

运行之后,如下图所示:

HttpUtility方法

类似于WebForm开发中一样,在Silverlight 2中同样提供了一一些HttpUtility方法,共有四个HtmlEncode、HtmlDecode、UrlEncode、UrlDecode,看一个简单的例子:

<Grid x:Name="LayoutRoot" Background="#CDFCAE">
<Grid.RowDefinitions>
<RowDefinition Height="75"></RowDefinition>
<RowDefinition Height="75"></RowDefinition>
<RowDefinition Height="75"></RowDefinition>
<RowDefinition Height="75"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="400"></ColumnDefinition>
<ColumnDefinition Width="200"></ColumnDefinition>
</Grid.ColumnDefinitions> <TextBox x:Name="txtHtmlEncode" Grid.Row="0" Grid.Column="0"
Width="300" Height="40"></TextBox>
<Button x:Name="btnHtmlEncode" Grid.Row="0" Grid.Column="1"
Background="Red" Width="120" Height="40" Content="HtmlEncode"
Click="btnHtmlEncode_Click"></Button> <TextBox x:Name="txtHtmlDecode" Grid.Row="1" Grid.Column="0"
Width="300" Height="40"></TextBox>
<Button x:Name="btnHtmlDecode" Grid.Row="1" Grid.Column="1"
Background="Red" Width="120" Height="40" Content="HtmlDecode"
Click="btnHtmlDecode_Click"></Button> <TextBox x:Name="txtUrlEncode" Grid.Row="2" Grid.Column="0"
Width="300" Height="40"></TextBox>
<Button x:Name="btnUrlEncode" Grid.Row="2" Grid.Column="1"
Background="Red" Width="120" Height="40" Content="UrlEncode"
Click="btnUrlEncode_Click"></Button> <TextBox x:Name="txtUrlDecode" Grid.Row="3" Grid.Column="0"
Width="300" Height="40"></TextBox>
<Button x:Name="btnUrlDecode" Grid.Row="3" Grid.Column="1"
Background="Red" Width="120" Height="40" Content="UrlDecode"
Click="btnUrlDecode_Click"></Button>
</Grid>

编写按钮处理事件:

private void btnHtmlEncode_Click(object sender, RoutedEventArgs e)
{
this.txtHtmlDecode.Text = HttpUtility.HtmlEncode(this.txtHtmlEncode.Text);
} private void btnHtmlDecode_Click(object sender, RoutedEventArgs e)
{
this.txtHtmlEncode.Text = HttpUtility.HtmlDecode(this.txtUrlDecode.Text);
} private void btnUrlEncode_Click(object sender, RoutedEventArgs e)
{
this.txtUrlDecode.Text = HttpUtility.UrlEncode(this.txtUrlEncode.Text);
} private void btnUrlDecode_Click(object sender, RoutedEventArgs e)
{
this.txtUrlEncode.Text = HttpUtility.UrlDecode(this.txtUrlDecode.Text);
}

运行后测试如下:

结束语

本文简单介绍了Silverlight 2与浏览器交互的相关辅助类方法。

作者:TerryLee
出处:http://terrylee.cnblogs.com 
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
 

一步一步学Silverlight 2系列(24):与浏览器交互相关辅助方法的更多相关文章

  1. 一步一步学Silverlight 2系列文章

    概述 由TerryLee编写的<Silverlight 2完美征程>一书,已经上市,在该系列文章的基础上补充了大量的内容,敬请关注.官方网站:http://www.dotneteye.cn ...

  2. 一步一步学Silverlight 2系列(32):图形图像综合实例—“功夫之王”剧照播放

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  3. 一步一步学Silverlight 2系列(31):图形图像综合实例—实现水中倒影效果

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  4. 一步一步学Silverlight 2系列(30):使用Transform实现更炫的效果(下)

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  5. 一步一步学Silverlight 2系列(29):使用Transform实现更炫的效果(上)

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  6. 一步一步学Silverlight 2系列(28):图片处理

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  7. 一步一步学Silverlight 2系列(27):使用Brush进行填充

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  8. 一步一步学Silverlight 2系列(26):基本图形

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  9. 一步一步学Silverlight 2系列(25):综合实例之Live Search

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

随机推荐

  1. intellij idea 和 myeclipse 转换

    原文出处:http://chinaxxren.iteye.com/blog/893970 当只用 intellij idea 建立 工程 1.首先是new project--->create p ...

  2. 一个强大的Android模拟器Genymotion

    相信很多Android开发者一定受够了速度慢.体验差效率及其地下的官方模拟器了,自己在平时的开发中几乎是不会用模拟器的,等的时间太久了,但是在一些尺寸适配或是兼容性测试的时候没有足够多的机器进行测试, ...

  3. eclipse软件安装及python工程建立

    原文地址:http://www.cnblogs.com/halfacre/archive/2012/07/22/2603848.html 安装python解释器 安装PyDev: 首先需要去Eclip ...

  4. 初涉Git/Github

    初涉Git/Github 第一部分:我的本次作业成果 我自己个人的github地址是:STRSong 我们开发团队小组的github地址是:三组 第二部分:给同学推荐github资源 推荐1 这个推荐 ...

  5. jquery+css实现邮箱自动补全

    今天在公司做一个电子商务网站的注册会员时,要求用户在电子邮箱文本框中输入时,给与热点提示常用的电子邮箱,帮助用户选择,提高体验效果.下面是用Jquery+css实现的邮箱自动补全,供大家参考和学习. ...

  6. BUPT复试专题—矩阵幂(2012)

    https://www.nowcoder.com/practice/31e539ab08f949a8bece2a7503e9319a?tpId=67&tqId=29638&rp=0&a ...

  7. Android入门常见问题

    前言: 眼下非常多人入门用android studio,可是我觉得这是 一个不好的開始. 一个集成的软件,不用你去部署.那么你就好难去学习究竟层的东西. 以后的问题时.问题所在还是在底层,就像&quo ...

  8. SQL SELECT TOP, LIMIT, ROWNUM 子句

    SQL SELECT TOP, LIMIT, ROWNUM 子句 SQL SELECT TOP 子句 SELECT TOP 子句用于规定要返回的记录的数目. SELECT TOP 子句对于拥有数千条记 ...

  9. iOS常用网络库收集

    一 ASIHttpRequest二 AFNetworking 三 AFDownloadRequestOperationA progressive download operation for AFNe ...

  10. PHP接收参数的几种方式

    PHP5在默认的情况下接收参数是需要使用 $_GET['value']; $_POST['value']; 还可以在PHP.ini 文件中的  将register_globals = Off  改re ...