With silverlight 3 RTM on the doorstep I thought it was time to take a look at one of the very cool new features in silverlight 3. I’m really excited about the new out-of-browser experience, since it allows customers to place the silverlight 3 application on their desktop instead of having to go to the website every time they want to work with the application.
In this article I’m going to show what an out-of-browser experience actually is and how you can configure a silverlight 3 application to make use of this new feature. Finally I’m going to talk about some of the things you need to keep in mind when working with out-of-browser experiences.
What is the out-of-browser experience in silverlight 3
New in silverlight 3 is the out-of-browser experience. With it you can detach your silverlight 3 application from the client browser and place a shortcut to the application in the start menu or on the desktop of the user’s computer.
Once the user installs the silverlight 3 application on his/her computer you no longer need a connection with the website or the internet at all to work with the silverlight application. Although you won’t be able to do much either, because most silverlight applications will need some kind of connection to a server backend to access data and services.
Creating an out-of-browser experience
The requirements for an out-of-browser experience are simple. The application will need to have the required identification tags in the manifest and that’s about it. For this you need to modify the manifest.xml inside the Properties folder of your silverlight 3 application project. The contents will look like the following snippet.
</p> <p><Deployment.ApplicationIdentity> <br />&nbsp;&nbsp;&nbsp; <ApplicationIdentity <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ShortName="Out of Browser Silverlight Application" <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Title="Out of Browser sample"> <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <ApplicationIdentity.Blurb>Description of your Silverlight application</ApplicationIdentity.Blurb> <br />&nbsp;&nbsp;&nbsp; </ApplicationIdentity> <br /></Deployment.ApplicationIdentity></p> <p>
After that you can boot the silverlight application and right click it to install it on the harddrive of the user. As the following picture demonstrates.
Once you click on the install option in the right-click menu, you will get a popup asking you where you want a shortcut to be made. It also contains some general information telling the user what is happening.
As you can see creating a basic out-of-browser experience is really simple. To make things really simple you can add a button to the application that has an event handler similar to the following code:
private void OnDetachClick(object sender, RoutedEventArgs e)
{
if (Application.Current.ExecutionState == ExecutionStates.RunningOnline)
{
bool success = Application.Current.Detach();
if (!success)
{
MessageBox.Show(“Cannot detach from the browser!”,
“Detach failed”, MessageBoxButton.OK);
}
}
}
This will tell the application to detach itself from the application. The boolean success is set to indicate whether the user allowed the application to detach.
Removing an installed silverlight 3 application
Once the silverlight 3 application is installed the user can remove it from his/her computer by right-clicking on the shortcut and choosing remove. The user can also open the application from the original website and select remove there.
Working offline
One of the things that typically happens to people is that they get disconnected from the internet. This is really annoying in a sence that the application won’t work when it’s silverlight. Detaching the silverlight 3 application from the browser partially solves this, but you need a bit more code inside the application to make it work offline.
Typically what you need is a way to cache data that needs to be transported to the server. Sadly there’s no standard solution for that at the moment. However there is away to detect whether the internet connection is working like it should be.
For this you need to attach an event handler to the NetworkChange.NetworkAddressChanged event. The following snippet demonstrates how to implement this event handler.
NetworkChange.NetworkAddressChanged += new EventHandler(OnNetworkAddressChanged);
void OnNetworkAddressChanged(object sender, EventArgs e)
{
if (NetworkInterface.GetIsNetworkAvailable())
{
// The application is online
}
else
{
// The network connection is lost
}
}
In this event handler you can switch between caching the updates and actually going online and send queued updates to the server when the internet connection is available again.
Receiving updates for the application
Another important aspect of the out-of-browser experience with silverlight 3 is the ability to receive updates for the XAP containing the application. This is actually pretty straight-forward.
Every time the application starts it will try to send a request to the original location from which it came and check for a response from the webserver which contains something other than HTTP 304 – not modified. If the webserver sends a response containing a new version of the XAP, that XAP will replace the one on the harddrive of the user and thus update itself.
On a side note: When working with silverlight 3 and the CompositeWPF remote module loader, you’re probably going to get in trouble here. I’m not sure at this point whether the module loader is capable of using the mechanics here. You have been warned… 🙂
Conclusion
The out-of-browser experience in silverlight 3 is really cool. I think this will make the platform a lot more interesting for developers looking to build light-weight, good looking rich client applications. It might not work for every scenario, but it’s surely something to take a look at when you’re building a silverlight 3 application.