Day #25: Silverlight Outside The Browser (Part 3 of 3)

If you’re wondering, I am certainly not writing these on the day they publish. These were written many, many days prior, and are only being published over the holidays. I’m spending my time celebrating with my family, exchanging gifts, and catching up on some sleep. Have a happy holiday season everyone.

Detecting A Network Connection

Today is the final part of a three part series on “Out of Browser”, which is a part of a larger series, the 31 Days of Silverlight.

We continued yesterday with a simple Silverlight project that had Out of Browser enabled, and was capable of installation and detected its state. You can download that out of browser application here. We’ll be adding to that project today.

Our next step is to determine whether a user has a network connection while running our application.

1. Checking the network connection

We have to add an additional namespace in order to work with our network connection, so let’s start there. Add this using statement to the top of your MainPage.xaml.cs file:

using System.Net.NetworkInformation;

Once we’ve added that, we can use the NetworkInterface API. Since we know we’re going to need to extract this code into its own method, let’s just do that out of the gate. I am creating a function that has an if statement checking another boolean value, this time to determine if there is a network connection. If there is, my network status image is green, and if not, the image is red. Here’s the entire method:

        private void CheckNetworkStatus()
{
if (NetworkInterface.GetIsNetworkAvailable())
{
networkimage.Source = new BitmapImage(new Uri("network_on.png", UriKind.Relative));
}
else
{
networkimage.Source = new BitmapImage(new Uri("network_off.png", UriKind.Relative));
}
}

2. Setting up an event handler for changes

Because our network status can change while we’re running the application, we need to make sure that we’ve subscribed to the NetworkAddressChanged event. This way, our application will always be aware of the current network status. Here’s my event handler code:

NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);

Hooking everything up

Now, we’ve got an CheckNetworkStatus() method, but it’s not being called yet. I’m actually going to call it from two places: once when our apoplication loads, and the other in the event handler method. Here’s what our final code looks like:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Net.NetworkInformation;

namespace SilverlightOutOfBrowser
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);
outofbrowserimage.MouseLeftButtonUp += new MouseButtonEventHandler(installBigBangTheory);
}

void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
{
CheckNetworkStatus();
}

void installBigBangTheory(object sender, MouseButtonEventArgs e)
{
if (Application.Current.InstallState == InstallState.NotInstalled)
{
Application.Current.Install();
}
}

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
checkOOB();
CheckNetworkStatus();
}

private void CheckNetworkStatus()
{
if (NetworkInterface.GetIsNetworkAvailable())
{
networkimage.Source = new BitmapImage(new Uri("network_on.png", UriKind.Relative));
}
else
{
networkimage.Source = new BitmapImage(new Uri("network_off.png", UriKind.Relative));
}
}

private void checkOOB()
{
if (Application.Current.IsRunningOutOfBrowser)
{
outofbrowserimage.Source = new BitmapImage(new Uri("outofbrowser_on.png", UriKind.Relative));
ToolTipService.SetToolTip(outofbrowserimage, "Big Bang Theory is installed on your machine.");
}
else
{
outofbrowserimage.Source = new BitmapImage(new Uri("outofbrowser_off.png", UriKind.Relative));
ToolTipService.SetToolTip(outofbrowserimage, "Install Big Bang Theory");
}
}
}
}

Summary

For many applications, network status may seem like something you don’t need. But if you’re talking to web services, data, etc., you absolutely need to know that those things are not available. In general, I’d recommend setting up a local data cache, and synchronizing that in the background, so that you can pass the data back and forth with the confidence that you won’t lose it if the user loses their connection. But that’s an article for another time.

Launch this completed Silverlight Out of Browser application.

You can download the final source code for this Out of Browser Silverlight project here.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s