Out of browser self updating – it couldn’t be easier!
January 8, 2010 Leave a comment

Peter Tweed was a Practice Lead for Technology Enablement in Slalom Consulting's San Franciso office when he wrote this post.
by Peter Tweed
One of the great features of Silverlight is that an application can be developed to be run in or outside of the browser. If it runs in the browser, the latest version of the application that is deployed to the web site is automatically used as it is downloaded immediately. What happens if the Silverlight application has the ability to run out of browser and the user has installed it locally? Historically it has always been a challenge to either write the solution yourself or in more recent years use click once for client side applications.
In Silverlight it is very easy to take care of when the application has the ability to run out of browser. This post will show you how to incorporate code to make the application check for and automatically download an updated version. Steps:
1. Create a new Silverlight project and in the project properties, select the enable running application out of the browser check box
2. In the grid in the MainPage.xaml file copy the following XAML
<br />
<Border BorderThickness="10" CornerRadius="10" BorderBrush="#FF934117"><br />
<Border.Background><br />
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"><br />
<GradientStop Color="#FFE80606" Offset="0" /><br />
<GradientStop Color="#FF8D0202" Offset="1" /><br />
</LinearGradientBrush><br />
</Border.Background><br />
<TextBlock Text="If an update is available a popup will appear!" VerticalAlignment="Center" FontSize="30" TextAlignment="Center" TextWrapping="Wrap" ><br />
</Border><br />
This code does nothing more than give a simple UI for the application.
3. Open the App.xaml.cs file
4. Replace the Application_Startup event handler with the following code
<br />
private void Application_Startup(object sender, StartupEventArgs e)<br />
{<br />
this.RootVisual = new MainPage();<br />
App.Current.CheckAndDownloadUpdateCompleted+=new CheckAndDownloadUpdateCompletedEventHandler(Current_CheckAndDownloadUpdateCompleted);<br />
App.Current.CheckAndDownloadUpdateAsync();<br />
}<br />
This code declares the event handler for the completed event for when the current application object checks for updates automatically and downloads any available update. The current application then checks for any updates and automatically downloads them.4. Paste the following function in the App class
<br />
void Current_CheckAndDownloadUpdateCompleted(object sender, CheckAndDownloadUpdateCompletedEventArgs e)<br />
{<br />
if (e.UpdateAvailable)<br />
{<br />
MessageBox.Show("An application update is available, please restart your application", "Update Available", MessageBoxButton.OK);<br />
}<br />
}<br />
This code defines the event handler to call when the application has checked for updates. The developer can interogate the event argument to see if an update was downloaded and if so perform some logic. In this case we inform the user they need to restart the application.
5. Run the application and right click it and select “Install application [Your App Name] onto this computer…”
6. Close the application that installed and ran on your computer and the browser version as well
7. Add a comment to the code in the app class and rebuild the application
8. Run the application installed on your computer
The application should raise the message box above telling you an update was automatically downloaded and you need to restart your application.
9. Restart your client installed application – you will not be shown a pop up about an automated update as none is available.
That’s it. Making your out of browser client installed Silverlight application automatically update and handle custom logic after an update has been downloaded is extremely simple. No deployment configuration is needed other than telling the application to run out of browser in the project properties window.
-Peter






