Saturday, November 13, 2010

Browser Information In Silverlight

In this post I will show you how to get the browser information.To get the browser information the namespace used for this is System.Windows.Browser. The BrowserInformation object contains properties such as the name, product name, product version , browser version etc. Let us start with the example which will use to get this information and display on the screen which I will share with you at the end of the post I will use two screen shot one for the Internet explorer and the second one for the Fire Fox.
For this Example I have create one class with the name BrowserInfo  which has the properties like BrowserName , BrowerVersion, Platform, ProductName, UserAgent of type string , IsCookiesEnabled of type Boolean to indicate whether Cookies are enabled or not , BrowserMinorVersion and BrowserMajorVersion are of type int.You can see the default constructor of the BrowserInfo class which is used to initialize all the properties of the class.

public BrowserInfo() { BrowserName = HtmlPage.BrowserInformation.Name; BrowserVersion = HtmlPage.BrowserInformation.BrowserVersion.ToString(); BrowserMajorVersion = HtmlPage.BrowserInformation.BrowserVersion.Major; BrowserMinorVersion = HtmlPage.BrowserInformation.BrowserVersion.Minor; IsCookiesEnabled = HtmlPage.BrowserInformation.CookiesEnabled; Platform = HtmlPage.BrowserInformation.Platform; ProductName = HtmlPage.BrowserInformation.ProductName; ProductVersion = HtmlPage.BrowserInformation.ProductVersion; UserAgent = HtmlPage.BrowserInformation.UserAgent; }
List 1

In the code you can see that BrowserName property is assigned the value of the Name in the BrowserInformation object which is the static member of the HtmlPage static class which is reside in the System.Windows.Browser namespace. The Name property of the BrowserInformation return the version of the browser technology that the current browser is based on.The browser technology name is typically different from the browser product name. CookiesEnabled property of BrowserInformation object indicate whether the browser supports cookies or not. The ProductName property of the BrowserInformation object get the product name of the browser.The browser product name is the familiar name of the browser, such as "FireFox".
You can see the output of the two different browser One for the internet explorer and second one is by using the Fire fox you can see the different properties of the two browsers.

Fire Fox
Internet Explorer
You can also see the Major and Minor version of the browser as shown in the screen shots. The UserAgent property will Gets the user agent string of the browser.The Platform property will return name of the operating system that the browser is running on. You can use these properties to developer application like the Google analysis or you can use them to show the trend of your site the user operation system the browser name etc.

All and any comments / bugs / suggestions are welcomed!

Tuesday, November 9, 2010

Reading And Writing Isolated Storage In Silverlight

In this post I will show you how you can create file on the Isolated Storage and also write on that file and also how can you check the size of the Isolated storage.Isolated storage is a mechanism that allows you to preserve data across browser sessions on a user’s machine. This storage area is tied to an individual user and helps you overcome the 4 KB limitation of a cookie. Unlike a cookie, isolated storage lies outside of the browser cache.
The code which is used to write in the Isolated Storage is listed in List 1.The class used for the Isolated Storage is IsolatedStorageFile which represents an isolated storage area containing files and directories. IsolatedStorageFile class has static method with the name GetUserStoreForApplication which is used to obtains user-scoped isolated storage corresponding to the calling code's application identity. After retrieving the user isolated storage object next step is to create the stream object which is used to write the contains.

using (IsolatedStorageFile isolatedStoragFile = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("MyFileName.txt", FileMode.Append, isolatedStoragFile)) { using (StreamWriter writer = new StreamWriter(stream)) { writer.Write("Message Date And Time: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") +" -> "+ txtTextToWrite.Text + "\r"); } stream.Close(); } }
List 1

IsolatedStorageFileStream is the class which Exposes a file within isolated storage.IsolatedStorageFileStream is used to read, write and create files in isolated storage. So if you want to read , write or create new file in the Isolated Storage area you need the object of the IsolatedStorageFileStream. I have passed the file name which in this case is .txt extension the, second parameter is the FileMode enumeration which has one of the values as listed in the table given below.The last parameter to the IsolatedStorageFileStream constructor is the IsolatedStorageFile object which is created before.

CreateNewAttempts to create a new file If a file of the same name exists, an IsolatedStorageException will be thrown. If there isn’t a preexisting file with the same name, a new, empty file will be created.
CreateA brute-force approach to creating a new file If a file of the same name exists, it’ll be overwritten. Either way, a new, empty file with the specified name will be created.
OpenAttempts to open a file with the given name If the file exists, the IsolatedStorageFileStream will have access to the file. If the file doesn’t exist, an IsolatedStorageException will be thrown.
OpenOrCreateOpens a file if it exists. If the file doesn’t exist, a new one will be created with the given name.
TruncateOpen an existing file and removes all its contents. This FileMode doesn’t allow read operations.
AppendOpens an existing file and prepares to add content onto the end
Table 1

After the creation of the IsolatedStorageFileStream object next is to use the StreamWriter class which take the IsolatedStorageFileStream object in the constructor and use the StreamWriter object to write the text or binary to the file.  The Image 1 show the layout of the example which I have used for the Isolated Storage. Here you can see that I have used two text boxes one for the input which user entered and second one is to read from the Isolated storage area which is shown in the Image 2.
Image 1

Image 2
The IsolatedStorageFile class has some useful properties which can be use to check the available free space (Gets a value that represents the amount of free space available for isolated storage.the value is in bytes), used space (Gets a value that represents the amount of the space used for isolated storage. the value is  in  bytes)and total allocated space to the current user.If you look at the example you can see that I have also print out the available free space and used space as well to show you how much space is available and how much is used.You can download the source code from here.

All and any comments / bugs / suggestions are welcomed!

Sunday, November 7, 2010

Reading Web.Config to pass values to silverlight Application

In application we often use to save the connection string and some other information like user name , web service URL etc in the web.config and then in application read them and use in the application and even we can easily changes these value at later time.However, this scenario does not work out of the box from a Silverlight application, which doesn’t seem to even have access to web.config.The reason is simple: a Silverlight application runs on the client side. The XAP -just like the browser- cannot access server resources such as web.config. In this post I will show you how you can read the web.config and pass to the silverlight application and then on the silverlight application how can you read the passed parameters.
Passing Parameters :
To pass the web.config parameter or other parameter to the silverlight is very easy you can pass the web.config or other value by assigning the InitParam parameter. For this you have to read the web.config values and store these values in the key value format.The code listed in the  List 1 show how to read the value from the web.config and paired them in key value format. Here you can see that I have read two values from the web.config one is the connection string and the second one is the user name. You see that each key value pair has equal to (=) sign between them.

< script runat="server"> public string WebDotConfig { get; set; } protected void Page_Load(object sender, EventArgs e) { WebDotConfig = "ConnectionString=" + ConfigurationManager.AppSettings["ConnectionString"]; WebDotConfig += ",UserName=" + ConfigurationManager.AppSettings["UserName"]; } < /script> //passing value <param name="initParams" value="<% =WebDotConfig%>" />
List 1
And for second value which is the user name you can see that comma (,) sign. as the comma(,) sign indicate new key value pair.And at the last statement which is written in the html side to pass the parameter to the silverlight application. Here you can see I have used the InitParams InitParams is one of the param name of the Silverlight plug-in object. Generally it is a dictionary object of type string. It contains a set of user defined key/value pairs. By default it is set as null or empty string and initializes at the time of first instance.

Reading InitParams :
 In this post I will show you 2 ways to read the InitParams on the silverlight application.

1- Application_Startup :
you can read the passed value in the Application_Startup event handler which is written in the App.xaml.cs file. The code to read the passed values on the silverlight application at the Application_Startup event handler is listed in List 2. Here you can see that StartupEventArgs e has property InitParams which is ready only of type IDictionary which takes two value of type string. Here I have first check the key which is the connection string and the user name and then I have read these values in the WebDotConfig class which has two properties with the ConnectionString and the UserName which are static.

private void Application_Startup(object sender, StartupEventArgs e) { if (e.InitParams != null) { if (e.InitParams.ContainsKey("ConnectionString")) WebDotConfig.ConnectionString = e.InitParams["ConnectionString"]; if (e.InitParams.ContainsKey("UserName")) WebDotConfig.UserName = e.InitParams["UserName"]; } RootVisual = new MainPage(); }
List 2

The reason for reading the values in the Application Startup event handler is the some time you need to use the Web service URL in you application. It is better to read it here and store it in some class which is accessible later when you need to use same URL to authenticate the user name. As Connection string and user name are static so one there are read you can use them any where in you application.

1- SilverlightHost :
In you don't the the variable which are used at the application start up like the web service URL which is used in common scenario. You can also read the passed parameter to the silverlight application by using the SilverlightHost class which has the InitParams property which is discussed in the Application Startup event handler.

private void UserControl_Loaded(object sender, RoutedEventArgs e) { if (Application.Current.Host.InitParams.ContainsKey("ConnectionString")) txtConnectionString.Text = Application.Current.Host.InitParams["ConnectionString"]; if (Application.Current.Host.InitParams.ContainsKey("UserName")) txtUserName.Text = Application.Current.Host.InitParams["UserName"]; }
List 3

In List 3 you can see I have used the InitParams of the SilverlightHost class and read the key values and assign them to the control which are used to display the passed parameter values. The output can been see in the Image 1, here I have displayed values read from both way from the Application startup event and also from the SilverlightHost class.

Image 1

I have separated both the section and place the Header for Application Statrup (Which displays values read at the Application Startup event handler and save in the WebDotConfig class, I have used binding for this as I have class which contain properties and bind to the respective controls on the user control)and for Host InitParams (Which are set at the code behind as I am reading values in the User control loaded event handler). .You can download the source code from here.  

All and any comments / bugs / suggestions are welcomed!

Saturday, November 6, 2010

Full Screen Mode In Silverlight

In this post I will show you how to make the silverlight application to run in full screen mode which is provided by the silverlight. You can use the F11 key to run the silverlight application in full screen mode but the toolbar button are visible on the top of screen in case of the F11 key. To go back to the embedded mode you can use the Esc key to exit the full screen mode. But if you want to give this functionality in you application or by using the code here is the code which is used to run in the Full screen mode and also to go back to the embedded mode.
For the code listed in List 1, the code is used written in the click event handler for the toggle button which will check the text based on the IsChecked value, if true mean application is running in embedded mode will changed to the full screen and set the text on the toggle button to Exit Full screen and same is for the when application is running in full screen in the else case.

private void btnFullScreen_Click(object sender, RoutedEventArgs e) { if (btnFullScreen.IsChecked.Value) { Application.Current.Host.Content.IsFullScreen = true; btnFullScreen.Content = "Exit Full Screen"; } else { Application.Current.Host.Content.IsFullScreen = false; btnFullScreen.Content = "Full Screen"; } }
List 1

Now let me tell you about the IsFullScreen property which is of Boolean type and you can see in the code that when set this property to true the application will run in full screen mode and when the IsFullScreen is set to false the application will come back to the embedded mode. The IsFullScreen is property of the Content which is property of the SilverlightHost class. The Content Property also has the FullScreenChanged event which is triggered every time you set the true or false to the IsFullScreen property. This event is helpful when you need to change the size location or some other properties of the controls when application is running in full screen or in embedded mode.

Image 1
Image one shows the effect of running the application in full screen mode when you changed from the embedded to the full screen mode the prompt will display the message to the user to change to the embedded mode you can press Esc key. When running application in full screen mode you have some limitation like the openFileDialog and showFileDialog classes are not supported.
Hope that you get some idea of how to change the full screen and back to the embedded mode by using the code. as It is very easy to change the mode from full screen to embedded and from embedded mode to full screen.

All and any comments / bugs / suggestions are welcomed!