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

Tagged with:
 

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

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>