Sunday, August 14, 2011

How can you detect Browser Information in Silverlight?

Sometime we need to detect the browser information of the user's computer where our Silverlight application is running. We can do this using the JavaScript but sometime it is useful to detect it from our Silverlight application. So, how to do it? Let us discuss this with a small simple example.

Read this post to know how to detect the browser information using the HtmlPage.BrowserInformation property.
Know about HtmlPage.BrowserInformation

Detecting browser information is not much difficult. You can use the inbuilt HtmlPage class available in the namespace called System.Windows.Browser. The class has static property called BrowserInformation of type BrowserInformation. This returns the following properties:

public sealed class BrowserInformation
    public string ProductName { get; }
    public string ProductVersion { get; }
    public string Name { get; }
    public Version BrowserVersion { get; }
    public bool CookiesEnabled { get; }
    public string Platform { get; }
    public string UserAgent { get; }

BrowserInformation class is a sealed class which returns browser name, product name, product version, browser version, platform, user agent and whether the browser has cookies enabled.
Implementing Code

Let us start with the code implementation. Let's create two properties in code behind of the page. We will create string properties called "Platform" and "BrowserInformation". Here is the code for your reference:

public string Platform
    get { return (string)GetValue(PlatformProperty); }
    set { SetValue(PlatformProperty, value); }
public static readonly DependencyProperty PlatformProperty =
    DependencyProperty.Register("Platform", typeof(string), typeof(MainPage), new PropertyMetadata(string.Empty));

public string BrowserInformation
    get { return (string)GetValue(BrowserInformationProperty); }
    set { SetValue(BrowserInformationProperty, value); }

Read more: .Net Zone
QR: how-can-you-detect-browser

Posted via email from Jasper-Net