Tuesday, March 29, 2011

Out Of Browser (OOB) - Part II : Silverlight

This post is the Part II of Out of Browser (OOB). The part I can be found here

In part I, we saw what is OOB, what are the ways to take the application offline and how the application gets the update from the server. In this part we will see an example. The example is simple one, we create an application that will tell us whether it is running in browser or out of browser, whether it is connected to internet or not.

Friday, March 25, 2011

Out Of Browser (OOB) - Part I : Silverlight

Out of Browser Feature
                      Silverlight 3 applications are no longer restricted to running in a browser. They can be run in a browser or it they can be detached from the browser and run from the desktop. These out-of-browser applications allow you to bring the richness of Silverlight 3 applications directly to the desktop without the restriction of running within a browser. With the release of Silverlight 3 Beta 1, Microsoft introduced the ability for Silverlight applications to run outside the browser. These “Out Of Browser” (OOB) applications are installed on the desktop, and have associated shortcuts just like traditional Windows applications. Silverlight OOB applications are still limited by the same security sandbox as their "in-browser" counterparts, so all the security and local access rules still apply as if they were running inside of a browser.

Offline Feature
                 While the OOB feature is really useful, Microsoft also introduced the capability to run OOB applications offline. There are several different implementations and options for Disconnected Silverlight OOB applications, but updating an existing Silverlight 3 application to run disconnected can be achieved in just three easy steps. We will look at these steps further in this document.

How OOB Works
Detaching the application:
               The first time users view the application, it will be in the web context in a page. If the application is enabled for OOB experiences, the user can detach the application. There are two ways through which you can detach the application to run in OOB mode, as follows:
1.    Through the right click context menu

2.      Via a user-initiated action (e.g. context menu or button click or some function in the application that calls Application.Current.Detach()).

This action takes the browser and creates an OOB application as follows.
  • The application (XAP) is requested again via the browser
  • The XAP gets stored locally in a low trust location along with metadata, which includes the origin URI of the XAP as well as metadata, most importantly for this discussion the ETag information of when it was downloaded (essentially a timestamp).

Launching the OOB application:
               If we close the application and launch it again from the desktop, it initiates a startup of the application. In this instance, the application looks at its metadata for the origin URI of the XAP and makes a request therewith. It compares the HTTP response data (code and ETag) for comparison. If the application wasn’t updated, then the application launches and all is well.  In fact, inspecting the request it would look something like this for our application:

GET /silverlight/oob/ClientBin/OutOfBrowser.xap HTTP/1.1
If-Modified-Since: Thu, 19 Mar 2009 03:52:35 GMT
User-Agent: Silverlight
Host: timheuer.com
X-P2P-PeerDist: Version=1.0
Accept-Encoding: peerdist
HTTP/1.1 304 Not Modified
Last-Modified: Thu, 19 Mar 2009 03:52:15 GMT
Accept-Ranges: bytes
ETag: "f2e3a81746a8c91:445"
X-Powered-By: ASP.NET
Date: Thu, 19 Mar 2009 03:55:18 GMT 

                        Here, in line no. 09, we can see the response “HTTP 304 Not Modified”. No further information is sent and we can see that no content was even delivered, because the application hasn’t changed. At the API level, we can detect the changes made in the application since its last run using the ExecutionStateChanged event handler, and checking if the current ExecutionState is ExecutionStates.DetachedUpdatesAvailable, in the App.xaml file.

public App()
    this.ExecutionStateChanged += new 
void App_ExecutionStateChanged(object sender, EventArgs e)
    if (App.Current.ExecutionState == 
        MessageBox.Show("Restart the app to use the latest version.");

                               In the above case, the ExecutionStateChanged event is not triggered on a state change of DetachedUpdatesAvailable.

Updating the OOB application
                Now suppose the application has been updated and uploaded to the server. The next time the user launches the application, the same requests happen.
               Again, the requests are made sending the metadata information. In this case, though, there is an update. What happens next is two-fold. Along with the response being sent with a new timestamp/ETag, the request also includes the bits of the updated application. Looking at the request, it would look like:

GET /silverlight/oob/ClientBin/OutOfBrowser.xap HTTP/1.1
If-Modified-Since: Thu, 19 Mar 2009 03:52:35 GMT
User-Agent: Silverlight
Host: timheuer.com
X-P2P-PeerDist: Version=1.0
Accept-Encoding: peerdist
HTTP/1.1 200 OK
Content-Length: 15557
Content-Type: application/x-silverlight-app
Last-Modified: Thu, 19 Mar 2009 03:56:29 GMT
Accept-Ranges: bytes
ETag: "ce39d0ae46a8c91:445"
X-Powered-By: ASP.NET
Date: Thu, 19 Mar 2009 03:56:45 GMT
 . . . 

                 Where “<data>” in the snippet above would actually be the bytes (notice the Content-Length and Content-Type headers) of the updated XAP. Two things happen now:
  • Application.Current.ExecutionState changes to a DetachedUpdatesAvailable state.
  • The XAP in the local storage location is updated to the new bits.
              Now right now there is no option to decline the updated bits. If the app is updated, the users will get them. Re-launching the application (from the local machine) would start up again with the newly downloaded bits, and the updated request would look again similar to the first:

GET /silverlight/oob/ClientBin/OutOfBrowser.xap HTTP/1.1
If-Modified-Since: Thu, 19 Mar 2009 03:56:49 GMT
User-Agent: Silverlight
Host: timheuer.com
X-P2P-PeerDist: Version=1.0
Accept-Encoding: peerdist
HTTP/1.1 304 Not Modified
Last-Modified: Thu, 19 Mar 2009 03:56:29 GMT
Accept-Ranges: bytes
ETag: "ce39d0ae46a8c91:445"
X-Powered-By: ASP.NET
Date: Thu, 19 Mar 2009 03:57:12 GMT

                    Thus, when an application is detached, metadata is stored about that application. At each launch, it does a check against that metadata to see if an update is visible. If the application is offline, it won’t block the application from launching.

In the next part, we will see an example.

Hope this helps. Happy Coding ! ! !

Out of Africa       ..A Kindle Browser HOMEPAGE ...ONE CLICK to NEWS, GMAIL, YAHOO mail, election coverage in Kindle's browser    Nintendo DS Browser    Nokia N900 Unlocked Phone/Mobile Computer with 3.5-Inch Touchscreen, QWERTY, 5 MP Camera, Maemo Browser, 32 GB--U.S. Version with Full Warranty  Skechers Men's Browser Casual Oxford,Black,11 M

Wednesday, March 23, 2011

Value Converter - Part II : Silverlight

This post is the Part II of Value Converter. The Part I can found here


In part I, we saw about the interface IValueConverter and the members of the interface i.e. Convert and ConvertBack. Now let’s see an example. In this example, we will see how to format the decimal value. First of all, let’s create a class that implements IValueConverter interface (let’s call this class Formatter).

Value Converter - Part I : Silverlight

              In Silverlight, when you’re binding data to controls there will be times when the data needs to be modified when it is binded to the control or when it goes back to the source property (during TwoWay binding).  You can always write code to change a given value, but in many cases it’s much easier to write a simple value converter instead that can be re-used. Although a separate property can be created in the source object that handles the formatting, a value converter can be created and re-used. To create a ValueConverter, first you need to add a class to your silverlight project and implement an interface IValueConverter. The IValueConverter interface defines two members Convert and ConvertBack. Convert is used to convert the data when it is propagating from the binding source to binding target i.e. from the source object to the control. ConvertBack work the other way round. 

Tuesday, March 22, 2011

Silent Indian National Anthem

Thursday, March 17, 2011

Null Coalescing operator - C#

v  One of the useful language features of C# is the ?? "null coalescing" operator.  This provides a nice, short way to check whether a value is null, and if so return an alternate value.

v  The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand.

v  ?? operator in Action

//If userId is not null then it will return userId value 
//If userId is null then it will return -1

int? userId = null;
int customerId = userId ?? -1; 
//[Output]: customerId = -1;            

int? userId = 12;
int customerId = userId ?? -1; 
//[Output]: customerId = 12;

//The ?? operator also works with reference types:

string param = "Hello, This is C#";
string message = param ?? "No message"; 
//[Output]: message = "Hello, This is C#"

Hope this helps. Happy Coding !

Earth Hour Beyond The Hour platform 3 mins version.mov

Wednesday, March 16, 2011

Don't sleep in office :-)

Funny clip, loved it and laughed like hell, so just wanted to share it with you all :-) nJoy.

How to : Use the UserState property while making async WCF service calls

                In Silverlight, all the WCF service calls are asynchronous. After the call to the WCF service is completed, a Completed event is called at the Silverlight UI side.Sometimes it becomes necessary to determine which task raised the Completed event.In this case you can use the "UserState" property to determine this.

Tuesday, March 15, 2011

Silverlight Load Process

The flow chart in the image below shows the load process for the Silverlight Applications. The flow chart is self explanatory.

Silverlight Load Process

Hope this helps. Happy Coding !

Saturday, March 12, 2011

Windows Authentication - Silverlight

The Windows Authentication can be implemented in two ways, First is by making a WCF service call and second is using JavaScript.

The First method, with detailed steps can be found on this link


The second method (which I feel is mush better/faster than first) is described below.

Thursday, March 10, 2011

Application_Exit event - App.xaml (Silverlight)

Before the Silverlight application exit, you can confirm whether the user really wants to exit or not.

Wednesday, March 9, 2011

Read and Display RSS Feeds - Silverlight

The Handbook of Loan Syndications and TradingThe SyndicationFeed class in Silverlight makes it easy to parse the feed response. The process of reading a feed is as simple as reading a RSS feed using the WebClient class, loading the stream into the SyndicationFeed class and binding it to the Silverlight UI.

To use the SyndicationFeed class you need to add reference to System.ServiceModel.Syndication

The steps are as follows,

Tuesday, March 8, 2011

Implicit Styling - Silverlight 4

Styling in Silverlight is similar to styling in HTML with CSS. It helps you keep your markup (HTML, XAML) code clean by putting all the properties into a separate file. At runtime, both the markup and the styles defined in the external style sheet are merged and give the look to the application as it was intended.

WPF 4 UnleashedStyling was already available in Silverlight. However, up until Silverlight 4, not all of the functions had been implemented (as compared to WPF). The most striking one was the default or implicit styling. Assume you are creating a Silverlight application and all controls of a specific type, for example all Buttons throughout the application should have a particular font size, font weight etc. In previous versions of Silverlight, we had to create a style and apply this style on every instance of the Button. There was no way to tell Silverlight that a particular style was supposed to be the style that had to be applied on all controls of a certain type.

Monday, March 7, 2011

Star Sizing in Grid Layout - Silverlight

Silverlight® 4: Problem - Design - Solution
In Silverlight, you have choice of using one of the following containers to create a layout for your application or control
  • Canvas
  • Grid
  • StackPanel
Most of us end up with Grid as container for all controls, unless you are developing applications that require precise placement of controls based on exact position co-ordinates or your creating come kind of animation. Therefore it is important that you understand how Grid layout works.

Tuesday, March 1, 2011

Microsoft's - Tag

Today, I came across a new concept or technology (I don't know what to call it technology or concept? ) from Microsoft - TAG. It is really cool. Read on to learn more about Microsoft Tag.

Microsoft Tag is a new kind of bar code that connects almost anything in the real world to information, entertainment, and interactive experiences on your mobile phone. Tags are free to create and use. You can add them to your ads, posters, product packages, display it on your website, billboards, clothing etc the list is endless. When you scan a Tag using the free Tag Reader application on your mobile phone, it will automatically open a webpage, add a contact to your address book, display a message, or dial a number – there are no long URLs to type or SMS messages to send, isn't it cool?