学Silverlight 2系列(24):与浏览器交互相关辅助方法
2008-10-03 11:34:33 来源:WEB开发网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与浏览器交互的相关辅助类方法。
Tags:Silverlight 系列 浏览器
编辑录入:爽爽 [复制链接] [打 印]- ››浏览器Cookie:一半广告 一半隐私
- ››silverlight全屏显示图片
- ››Silverlight MVVM 模式(一) 切近实战
- ››浏览器Skyfire将推出iPad版,支持Flash
- ››Silverlight for Windows Phone 7开发系列(1):...
- ››Silverlight for Windows Phone 7开发系列(2):...
- ››Silverlight for Windows Phone 7开发系列(3):...
- ››Silverlight for Windows Phone 7开发系列(4):...
- ››浏览器中的内存泄露(重新整理ing)
- ››浏览器已经去过多少个网站
- ››Silverlight for Symbian
- ››Silverlight3系列(四)数据绑定 Data Binding 1
更多精彩
赞助商链接