15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1114 ❘ ChaPTer 38 silverliGht<br />

<br />

<br />

code snippet OutOfBrowserDemo/MainPage.xaml<br />

The constructor MainPage checks whether the application is running out of browser or within the browser<br />

<strong>and</strong> sets the text of the TextBlock control accordingly. To check if the application is running out of<br />

browser, you can use the IsRunningOutOfBrowser property of the Application class. This class also<br />

provides information about the installation state of the application with the InstallState property.<br />

Possible values are NotInstalled, Installing, Installed, <strong>and</strong> InstallFailed with the InstallState<br />

enumeration. Using this information, the installButton <strong>and</strong> updateButton controls are visible<br />

or collapsed.<br />

public MainPage()<br />

{<br />

InitializeComponent();<br />

if (App.Current.IsRunningOutOfBrowser)<br />

{<br />

text1.Text = "running out of browser";<br />

}<br />

else<br />

{<br />

text1.Text = "running in the browser";<br />

updateButton.Visibility = Visibility.Collapsed;<br />

}<br />

if (App.Current.InstallState == InstallState.Installed)<br />

{<br />

installButton.Visibility = Visibility.Collapsed;<br />

}<br />

}<br />

code snippet OutOfBrowserDemo/MainPage.xaml.cs<br />

The event h<strong>and</strong>ler OnInstall() is invoked by clicking the associated button. An installation happens just<br />

by invoking the Install() method of the Application class.<br />

private void OnInstall(object sender, RoutedEventArgs e)<br />

{<br />

if (App.Current.InstallState == InstallState.NotInstalled)<br />

{<br />

bool result = App.Current.Install();<br />

if (result)<br />

text1.Text = "installation successful";<br />

}<br />

}<br />

Checking for an update on the server can be done programmatically with the Application class as well.<br />

Here, the update is done in the event h<strong>and</strong>ler method OnUpdate(). CheckAndDownloadUpdateAsync()<br />

checks for an available update <strong>and</strong> installs the update. This is an asynchronous operation using the async<br />

component pattern that fires the event CheckAndDownloadUpdateCompleted on completion of the update.<br />

Success or error information is written in the h<strong>and</strong>ler of this event. After a successful update, the new<br />

version is available on the next start.<br />

private void OnUpdate(object sender, RoutedEventArgs e)<br />

{<br />

App.Current.CheckAndDownloadUpdateCompleted += (sender1, e1) =><br />

{<br />

if (e1.Error != null)<br />

{<br />

text1.Text = e1.Error.Message;<br />

}<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!