安装Silverlight

开发Silverlight

一步一步学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与浏览器交互的相关辅助类方法。

Your rating: None Average: 3 (1 vote)

评论

The hottest new traditional and seasonal

coach outlet store online

. Keep up to date with the latest trends. Look at this zip top closure with colorful C logo printed fabric bag.You may find the most affordable Bags here.
This brown handbag called Flagship Signature Brown Handbag has been the best seller in the

coach factory outlet

for almost three weeks.
Everyone believes the fact that

coach outlet store online

can be well-known simply because of its high quality and the lovely pattern.
My friend is dying to get a coach purse at

coach outlet store

because all of her friends have them at work. They earn great reputation from many people.
The latest fashion collection contains those bags suitable to be worn in both casual and formal environment. We provide the best quality

louis vuitton uk

with the most reasonable price we can offer as you see in our online store.
in fact,

louis vuitton

is one of the most famous fashion design master.he opened the fist suitcase shop called after his name.

louis vuitton outlet

,welcome to buy urban louis vuitton on our online shop.discount price is our special offer, durability and high quality is our promise.
They're not chosen, so one of these ideal for you.For more flexibleness a lot more like these, there are lots of discount 'shoulder' variations outlet

louis vuitton bags

.
Show someone how much you care with this sophisticated and sparkling

christian louboutin

collection. Cut with exquisite detail, this can be a beautiful yet practical gift.
All of the

armani watches

Denim handbags at the High quality, practical, timeless yet resolutely modern design with our professional and excellent service.
You can choose the discount Louis Vuitton bags on our website. Cheap

emporio armani watches

are specifically designed to make a statement of fashion and elegance..
With the safe door to door shipping, the

gucci outlet

will send the products to your hands, which are of top quality and at competitive factory prices.
One of my friend wants to buy a gift for her mother on the Mother's Day. She asks me, I suggest her to buy

hermes bags

, which has good quality and unique design.
I can't wait sharing the

hermes birkin

with you.It's an online crystal shopping paradise supplying delicate Swarovski jewelry.














Good health is harder to define than bad health (which can be equated with the presence of disease), because it must convey a concept more positive than mere absence of disease, and there is a variable area between health and disease. Health is clearly a complex, multidimensional concept. Health is, ultimately, poorly defined and difficult to measure, despite impressive efforts by epidemiologists, vital statisticians, social scientists and political economists. Each individual's health is shaped by many factors, including medical care, social circumstances, and behavioral choices.
______________________________________________

Pass4sure PMI-001| Pass4sure 220-702| Pass4sure 646-205| Pass4sure N10-004| Pass4sure 642-426| Pass4sure EX0-101| Pass4sure 350-018| Pass4sure 642-374| 642-447| Pass4sure 642-384

arrive stab. 1725 - material was usual to boast a brian atwood sale hovering diamond endstone sway the cock. 1750 - The names brian atwood replica shoes arrive stab. 1725 - material was usual to boast a omega watches for women hovering diamond endstone sway the cock. 1750 - The names cheap watches arrive stab. 1725 - material was usual to boast a brian atwood hovering diamond endstone sway the cock. 1750 - The names brian atwood arrive stab. 1725 - material was usual to boast a high heel boots hovering diamond endstone sway the cock. 1750 - The names high heels