WEB开发网
开发学院图形图像Flash 学Silverlight 2系列(24):与浏览器交互相关辅助... 阅读

学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.Na

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

运行之后,如下图所示:

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

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系列(24):与浏览器交互相关辅助方法

结束语

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

上一页  1 2 

Tags:Silverlight 系列 浏览器

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接