Wednesday, August 25, 2010
Auto-Updating a Silverlight Out-Of-Browser App
Silverlight supports Out-Of-Browser operation, meaning you can build an app that runs very much like a desktop application. You enable this via a simple setting in the properties of the Silverlight control. Once you have an OOB app, you can either start it as a stand alone app, or, if the app is originally launched within a browser as part of a page, users can right-click on it and choose to “install the app locally”.
I really like this feature. But I was surprised to learn that an app taken out of the browser does not automatically update itself when a newer version is available on the server. It always seems to me that is the quintessential modus operandi in Silverlight. After all, in the web browser, when you navigate to a site with a Silverlight control, you automatically and always get the latest version of that control. Versions that have been cashed from prior visits are only used if the version is still up to date. Not so for OOB Silverlight apps. You can publish new versions all you want, the client (by default) has the version that was originally installed.
So what do you do to fix this problem? Well, luckily it turns out you can simply add a few lines of code to the app’s constructor to enable updating. Add the following code to your App class’ constructor (in App.xaml.cs) to get auto-updating:
if (Current.IsRunningOutOfBrowser)
{
Current.CheckAndDownloadUpdateCompleted += (sender, e) =>
{
if (e.Error == null && e.UpdateAvailable)
MessageBox.Show("New version! Please restart!");
};
Current.CheckAndDownloadUpdateAsync();
}
So the whole constructor part of the app class should now look like this:
public partial class App : Application
{
public App()
{
Startup += Application_Startup;
Exit += Application_Exit;
UnhandledException += Application_UnhandledException;
InitializeComponent();
if (Current.IsRunningOutOfBrowser)
{
Current.CheckAndDownloadUpdateCompleted += (sender, e) =>
{
if (e.Error == null && e.UpdateAvailable)
MessageBox.Show("New version! Please restart!");
};
Current.CheckAndDownloadUpdateAsync();
}
}
// …
There you go! Live is good again :-)
Posted @ 10:13 AM by Egger, Markus (markus@code-magazine.com)