Wednesday, February 9, 2011

Auto updating a Silverlight 4.0 OOB Application

It's really simple to set up your Silverlight 4.0 OOB Application to automatically update itself.

Step 1 - set up the CheckAndDownloadUpdateCompleted event:
In the App.Xaml class, add the following to the constructor:


Current.CheckAndDownloadUpdateCompleted += Current_CheckAndDownloadUpdateCompleted;


Now, make a call to check for an update and download if necessary:


Current.CheckAndDownloadUpdateAsync();


Step 2 - Handle the CheckAndDownloadUpdateCompleted event:


While it is not essential to handle this event, it is recommended to let your users know that a new version of the application has been downloaded, or even write it to the log file.



private void Current_CheckAndDownloadUpdateCompleted(object sender, CheckAndDownloadUpdateCompletedEventArgs e)
        {
            if (e.UpdateAvailable)
            {
                Logger.WriteLine(
                    "An application update has been downloaded and will be available on restart of the application");
                
//ADD CODE HERE TO ALERT THE USER AS REQUIRED


                }
            }
            else if (e.Error != null)
            {
                Logger.WriteLine(
                    "An application update is available, but an error has occurred.\nThis can happen, for example, when the update requires\na new version of Silverlight or requires elevated trust.\n" +
                    "To install the update, visit the application home page.");
            }
            else
            {
                Logger.WriteLine("There is no update available.");
            }
        }


And thats it!
A downfall of this is that there is no way to check for an update and only download it if the user says "Yes". Its either all or nothing.

No comments:

Post a Comment