How to Detect Network Changes Using Silverlight

How to Detect Network Changes Using Silverlight

In my current application, I got a requirement to stop the uploading of data once the network connection is lost and resume uploading again once the network connection was up. This was a very big and serious issue for me and after doing some experiment I found some useful events of browser that would help me resolve the issue, but my application uses Silverlight Technology. After doing some research on google, I found a feature of Silverlight known as ‘Wrap Up’ belonging to the NetworkChange and NetworkInterface classes.

NetworkChange allows applications to receive notifications whenthe Internet Protocol (IP) address of a network interface, also called a network card or adapter, changes. NetworkInterface provides availability of information for a network interface. Here is a step by step instruction guide to get this in action:

Step 1: Create a Silverlight application and open the Mainpage.xml file

Step 2: Add the reference of wrapper System.Net.NetworkInformation;

Step 3: Add the event under the MainPage constructor and update the main code as mentioned below:

 public MainPage(){
//This will run each time
when the network status will chnaged
Loaded += newRoutedEventHandler(MainPage_Loaded);
MainPage_Loaded(object sender, RoutedEventArgs e)
+= new                NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);

Step 4: Create the GetNetworkStatus() method and event  NetworkChange_NetworkAddressChanged

to see the changes in action. Update the main page code as mention below:

Online { get; set; }void
NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
Online = NetworkInterface.GetIsNetworkAvailable();
TextBoxStatus.Text = Online ? “Online” : “Offline…”;
scb = newSolidColorBrush(Online? Colors.Green : Colors.Red);
LayoutRoot.Background = scb;

TheGetNetworkStatus method is called whenever the network changes. A call toNetworkInterface.GetIsNetworkAvailable indicates whether any network connection is available. If a network is available, the page will be updated to indicate this by display a yellow background with the word Online. If the network is not available, the background color will be red with the word Offline.

Run the application and see the result. This helps me to detect the network connection and now I will implement this my file unloader plugin and share this in my next article.

Please give your comments if you found this helpful or not, will help me to learn more.

  • How to Detect Network Changes,
  • networkchange,
  • networkinterface,
  • silverlight technology,
  • silverlight wrapup,
  • Leave a Reply