Friday 10 June 2011

Updating Silverlight OOB Applications

In Silverlight Out Of Browser applications (OOB), it’s easy to give ClickOnce style new version updates to deployed apps.  Just add the following code to your App.xaml.cs file:

public App()
{
this.Startup += this.Application_Startup;

InitializeComponent();

if (!Application.Current.IsRunningOutOfBrowser) return;

Application.Current.CheckAndDownloadUpdateCompleted += this.CheckAndDownloadUpdateCompleted;
Application.Current.CheckAndDownloadUpdateAsync();
}

new void CheckAndDownloadUpdateCompleted(object sender, CheckAndDownloadUpdateCompletedEventArgs e)
{
if (e.Error == null && e.UpdateAvailable)
{
MessageBox.Show("Application updated, please restart to apply changes.");
}
}

private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
}

To show this working, I’ve created an app that simply displays it’s current version (set in the Properties/AssemblyInfo.cs file.


ScreenShot081


Here’s the code behind for the MainPage.xaml.cs:

using System;
using System.Reflection;
using System.Windows.Controls;

namespace UpdatingOOB
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();

Assembly assembly = Assembly.GetExecutingAssembly();
var assemblyName = new AssemblyName(assembly.FullName);
Version version = assemblyName.Version;

tbVersionInfo.Text = string.Format("Version: {0}.{1}.{2}.{3}",
version.Major,
version.Minor,
version.Build,
version.Revision);
}
}
}

Change code (or just the version number), rebuild your Silverlight app source code and re-run your Silverlight host web project to push your updated xap file to the host.


Now when you run your Silverlight OOB application the update is detected and message displayed.


ScreenShot082


When you restart the Silverlight OOB the new version runs.


ScreenShot083


You can download the code here:


http://stevenhollidge.com/blog-source-code/UpdatingOOB.zip

No comments:

Post a Comment