Home / Programming / Blog article: Get Browser Information In Silverlight

| RSS

Get Browser Information In Silverlight

July 9th, 2009 | 1 Comment | Posted in Programming

While writing Silverlight you may come across a need to get information about host browsers within your application. In Silverlight this information is exposed by BrowserInformation class within System.Windows.Browser namespace. BrowserInformation can be accessed through HtmlPage class which also lives in System.Windows.Browser namespace. Here is an example where I am retrieving information about the browser.

<StackPanel x:Name="LayoutRoot"
            Margin="10"
            Background="PaleGoldenrod">

  <StackPanel.Resources>
    <Style x:Key="TextBlockStyle"
           TargetType="TextBlock">
             <Setter
               Property="FontSize"
               Value="14" />
             </Style>
  </StackPanel.Resources>

  <TextBlock
    Text="Information about your browser"
    HorizontalAlignment="Center"
    Style="{StaticResource TextBlockStyle}" />
  <controls:WrapPanel>
    <TextBlock
      Text="You are using "
      Style="{StaticResource TextBlockStyle}"/>
    <TextBlock
      Text="{Binding Name}"
      Style="{StaticResource TextBlockStyle}" />
    <TextBlock
      Text=". Version "
      Style="{StaticResource TextBlockStyle}" />
    <TextBlock
      Text="{Binding BrowserVersion}"
      Style="{StaticResource TextBlockStyle}" />
    <TextBlock
      Text="Your platform is "
      Style="{StaticResource TextBlockStyle}"/>
    <TextBlock
      Text="{Binding Platform}"
      Style="{StaticResource TextBlockStyle}"/>
  </controls:WrapPanel>
</StackPanel>

 

We also need to assign a DataContext for our StackPanel to BrowserInformation object.

LayoutRoot.DataContext =
  System.Windows.Browser.HtmlPage.BrowserInformation;

 

And the output on my machine is.

image

Leave a Reply 3902 views, 1 so far today |
Follow Discussion

One Response to “Get Browser Information In Silverlight”

  1. Pinal Bhatt Says:

    HtmlPage.BrowserInformation does not work as expected.

    For FireFox and Chrome : HtmlPage.BrowserInformation.Name returns “netscape”

    On IE 8 : HtmlPage.BrowserInformation.BrowserVersion returns 4.0

    This is not what i want.

    I need to know actual name and version of the browser on which my SL app is running.

Leave a Reply





Switch to our mobile site