一步一步学Silverlight :与浏览器交互相关辅助方法
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与浏览器交互的相关辅助类方法。
[第1页][第2页]