With the release of Silverlight 3 Beta 1, Microsoft introduced the ability for Silverlight applications to run out of the browser. These "out of browser", or OOB for short, Silverlight applications are installed and have associated shortcuts just like traditional Windows applications. OOB Silverlight 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.
As cool as the ability to run Silverlight application out of the browser is, Microsoft went one better by introducing functionality to run OOB applications offline. There are several different implementations and options for OOB disconnected Silverlight applications, but updating an existing Silverlight 3 application to run disconnected can be achieved in just three easy steps.
Step one: Configure the Silverlight application to enable OOB
Users can't install and run your application out-of-browser unless you allow it, so your first step is to configure the application to enable OOB. You do this by making some small updates to the application manifest XML file that's part of your Silverlight project. These updates include, but are not limited to, values for the name of the application, which shows in the title bar of the application window and a list of icons to display in various places.
That's it for step one; now the user will be able to right click on the running application and choose to install it locally. Alternatively, you could code your application adding an event, such as one triggered by a button click, to programmatically spawn the install dialog. This is done by calling Application.Current.Detach().
Step two: Test to see if you have an active network connection
OOB applications can run with or without an active network connection, so step two is to check the state of the user's network connection. You do this by calling the NetworkInterface.GetIsNetworkAvailable() method. This is a check to perform before making any Web service calls, since they would fail without a valid network connection. In addition to the GetIsNetworkAvailable method, you can also subscribe to the NetworkChange .NetworkAddressChanged event of the class. This event will fire anytime there is a change in a network connection's status.
Step three: Save data locally or remotely in Silverlight
The third and final step is to update your application to load and save data locally or remotely, depending on the state of the user's network connection. If your application is running with an active network connection, you should load and save as you would normally. In the case where your application is running disconnected, there are a couple of options for loading and saving data. One is to provide the user with a manual option for loading and saving data using the OpenFileDialog and SaveFileDialog classes, respectively.
A much better option, in order to give the users a seamless experience whether they're connected or not, is to use isolated storage to cache data. Caching data allows the application to load and save data using isolated storage when running disconnected. This option allows the user experience to remain seamless regardless of the connected or disconnected state of your application.
That's all it takes to implement a disconnected, out-of-browser (OOB) Silverlight 3 application.
Steven Porter is a Senior Consultant and Project Manager for Wintellect, a
consulting/debugging and training firm founded by industry experts Jeff Prosise, Jeffrey Richter,
and John Robbins. He specializes in technologies such as ASP.NET, ASP.NET AJAX, Silverlight, and
WPF. Steve is a Microsoft MVP in Client Application Development and a frequent speaker at events
such as user groups and code camps on topics ranging from Silverlight to Windows Workflow
This was first published in April 2009