Silverlight的操作方法非常灵活,要想熟练的掌握这一工具的应用并不是一件非常容易的事情。在实际操作中我们需要不断的积累应用经验,以求达到一个运用熟练的程度,方便我们的程序开发。比如Silverlight获取浏览器信息的技巧等。#t#
在Silverlight 2中提供了获取浏览器信息的一个类BrowserInformation,可供我们直接调用,如Silverlight获取浏览器信息中的名称及浏览器版本,是否禁用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;
- }
Silverlight获取浏览器信息就为大家介绍这里。