|
概述
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>
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與瀏覽器交互的相關輔助類方法。
NET技術:一步一步學Silverlight :與瀏覽器交互相關輔助方法,轉載需保留來源!
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。