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.
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.
When you restart the Silverlight OOB the new version runs.
You can download the code here:
No comments:
Post a Comment