Monday, August 3, 2009

TechEd 2009: Silverlight 3 Part 1: Out of Browser

As promised I am here writing about the nice things I have seen so far at TechEd 2009. I am so dedicated that I am in my hotel room after day 1 doing this :). I am going to start off with writing on the “What’s new in Silverlight 3” presentation.

There are some very nice new features in Silverlight 3. I will start off with the Out of Browser support of Silverlight. Some people may have heard of this before but I will just show what the final verdict on was between the beta phases and final version. As you might have suspected, Out of Browser allows the user to detach (install) the Silverlight application from the web browser and run it from your desktop. There is no fundamental change in how Silverlight works – it still runs out of the sandbox.

This is quite a simple and straight forward technique. You create a Silverlight application, go to properties and mark “Enable running application out of browser”. You can then select the “Out of Browser Settings” button. This button will allow you to specify details about the Silverlight app when running out of browser like Name, Width, Height, Icon, etc. Once the user browses to the Silverlight web site, he is able to right click the application and select to install the application.

There is some additional functionality that you can provide. There is support for manual installation of application and determining if the application is already installed. There is also support for the application to determine when network connectivity changes or IP address changes. This will then allow the application to store data offline (in Isolated storage) until the application is online again. Once online, the user can be prompted to synchronize data back to the server. The isolated storage space has also been increased from 1MB to 25MB.

There is also support for the application to check the server if new updates are available for the application.


Here is an example:

Application app = Application.Current;

public MainPage()
{
InitializeComponent();
LayoutRoot.DataContext = Deployment.Current.OutOfBrowserSettings;
UpdateUI();
app.CheckAndDownloadUpdateCompleted += App_CheckAndDownloadUpdateCompleted;
app.InstallStateChanged += (s, e) => UpdateUI();
NetworkChange.NetworkAddressChanged += (s, e) => UpdateNetworkIndicator();
}

private void UpdateUI()
{
UpdateNetworkIndicator();

installButton.Visibility =
app.InstallState == InstallState.NotInstalled ?
Visibility.Visible : Visibility.Collapsed;

updateButton.Visibility =
app.IsRunningOutOfBrowser ?
Visibility.Visible : Visibility.Collapsed;

isRunningOutOfBrowserTextBlock.Text = app.IsRunningOutOfBrowser.ToString();

installStateTextBlock.Text = app.InstallState.ToString();
}

private void UpdateNetworkIndicator()
{
networkIndicator.Visibility =
app.IsRunningOutOfBrowser ?
Visibility.Visible : Visibility.Collapsed;

bool online = NetworkInterface.GetIsNetworkAvailable();

networkIndicator.Text = online ? "ONLINE" : "OFFLINE";

updateButton.Visibility = online ? Visibility.Visible : Visibility.Collapsed;
}

private void installButton_Click(object sender, RoutedEventArgs e)
{
try
{
app.Install();
}
catch (InvalidOperationException)
{
MessageBox.Show("The application is already installed.");
}
}

private void updateButton_Click(object sender, RoutedEventArgs e)
{
app.CheckAndDownloadUpdateAsync();
}

private void App_CheckAndDownloadUpdateCompleted(object sender, CheckAndDownloadUpdateCompletedEventArgs e)
{
if (e.UpdateAvailable)
{
MessageBox.Show("An application update has been downloaded. Restart the application to run the new version.");
}
else if (e.Error != null && e.Error is PlatformNotSupportedException)
{
MessageBox.Show("An application update is available, but it requires a new version of Silverlight. " +
"Visit the application home page to upgrade.");
}
else
{
MessageBox.Show("There is no update available.");
}
}


More information can be found here at the presenter’s (Eben de Wit) blog. This blog also contains the slides and code for all the new features of Silverlight 3.

No comments:

Post a Comment