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

版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。 http://terrylee.blog.51cto.com/342737/67271

概述

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.blog.51cto.com/342737/67271

转载于:https://www.cnblogs.com/hdjjun/archive/2008/12/24/1361460.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值