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

概述

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;
}

运行之后,如下图所示:

TerryLee_Silverlight2_0113  

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);
}

运行后测试如下:

TerryLee_Silverlight2_0114

结束语

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

作者:TerryLee
出处:http://terrylee.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则视为侵权。
Tag标签: Silverlight

posted on 2008-03-15 17:08 TerryLee 阅读(4495) 评论(8)  编辑 收藏 所属分类: Silverlight

评论

#1楼  2008-03-15 17:58 飞雪尔 [未注册用户]

browser version估计是识别成了Mozilla 4.0 吧。   回复  引用    

#2楼 [楼主] 2008-03-15 18:02 TerryLee      

@飞雪尔
不太清楚,我用的IE8   回复  引用  查看    

#3楼  2008-05-26 23:07 g6666 [未注册用户]

good!   回复  引用    


标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2008-03-15 17:10 编辑过
 

另存  打印
 


导航

公告

  • 网名:TerryLee
  • 本名:李会军
  • 位置:中国北京 Ethos
  • 联系方式:
  • 访问我的个人主页

 MVP配置

 版权声明

  • 本站采用创作共用许可 署名,非商业

绿色通道

IT新闻

统计

与我联系

留言簿(322)

我的标签

随笔分类

随笔档案

个人站点

关注项目

好的网站

我的好友

搜索

积分与排名

阅读排行榜

评论排行榜