DeviceNetworkInformation.NetworkAvailabilityChanged Event

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Occurs when the availability of the network changes. For example, an application can register for this event and get a notification if a WiFi network becomes available. The application could then adjust its behavior to take advantage of this high bandwidth network interface.

Namespace:  Microsoft.Phone.Net.NetworkInformation
Assembly:  Microsoft.Phone (in Microsoft.Phone.dll)

Syntax

Public Shared Event NetworkAvailabilityChanged As EventHandler(Of NetworkNotificationEventArgs)
public static event EventHandler<NetworkNotificationEventArgs> NetworkAvailabilityChanged

Remarks

The DeviceNetworkInformation class raises this event for network interface changes such as connecting, disconnecting, and changing roaming status.

To associate this event with a method that will handle the event, create a delegate and add an instance of the delegate to the event. The event handler is called whenever this event occurs, unless you remove the delegate. If an application resumes from a dormant state, the framework automatically re-registers the callback. In other words, you do not have to register for the event again.

For more information about handling events, see Consuming Events.

Examples

The following code example shows how to subscribe to the NetworkAvailabilityChanged event and determine what the changes were. It uses the NotificationType and NetworkInterface properties of the NetworkNotificationEventArgs to show the type of notification that was received as well as the name of the network interface for which the notification was received.

This example assumes you have a TextBlock in your application called textBlock1. This example also assumes that you have added the following using statements to your code window.

using Microsoft.Phone.Net.NetworkInformation;
using System.Windows;
using Microsoft.Phone.Tasks;
// Constructor
public MainPage()
{
   InitializeComponent();

   // Subscribe to the NetworkAvailabilityChanged event
   DeviceNetworkInformation.NetworkAvailabilityChanged += new EventHandler<NetworkNotificationEventArgs>(NetworkAvailabilityChanged);
}

void NetworkAvailabilityChanged(object sender, NetworkNotificationEventArgs e)
{

   string change = string.Empty;
   switch (e.NotificationType)
   {
      case NetworkNotificationType.InterfaceConnected:
         change = "Connected to ";
         break;
      case NetworkNotificationType.InterfaceDisconnected:
         change = "Disconnected from ";
         break;
      case NetworkNotificationType.CharacteristicUpdate:
         change = "Characteristics changed for ";
         break;
      default:
         change = "Unknown change with ";
         break;
   }

   string changeInformation = String.Format(" {0} {1} {2} ({3})", 
         DateTime.Now.ToString(), change, e.NetworkInterface.InterfaceName, 
         e.NetworkInterface.InterfaceType.ToString());

   // Interacting with UI, so make sure this is done on the UI thread.
   Dispatcher.BeginInvoke(() =>
   {
      textBlock1.Text += changeInformation;
      textBlock1.Text += Environment.NewLine;
   });  
}

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1

Platforms

Windows Phone

See Also

Reference

DeviceNetworkInformation Class

Microsoft.Phone.Net.NetworkInformation Namespace

Other Resources

How to detect network changes for Windows Phone 8