Sometime we need to know whether Network connectivity is available or not from our application. In Windows Phone 7, we have the required API exposed for the developers to detect the status of the network connection and take respective step based on that.

 

So how to detect it? In this small tip of the tutorial series, we will learn about the API and we will also see how this can be done using a small demo which includes code too. Read to know more about it.

 

Index - Windows Phone 7 (Mango) Tutorial

 

Know about the APIs

Let's discuss about the API first. NetworkInterface class present inside the Microsoft.Phone.Net.NetworkInformation namespace is responsible to inform you whether the network connection is available or not. NetworkInterface class has a method called GetIsNetworkAvailable(). Once called it will return boolean value true or false based on the current network availability.

 

Microsoft.Phone.Net.NetworkInformation.NetworkInterface class inherits from the base class System.Net.NetworkInformation.NetworkInterface class. Find the complete Meta Data of the classes here:

 

Meta Data of Microsoft.Phone.Net.NetworkInformation.NetworkInterface

 

Meta Data of System.Net.NetworkInformation.NetworkInterface

 

Modifying the Code

It's time to create a small demo application to showcase the same. This will clarify many things. Let's create a new Windows Phone 7 application project. Now inside the MainPage.xaml, we will create a DependencyProperty "NetworkAvailability" of type string which will return the text string displaying whether the network is available or not.

 

Inside the constructor, we will call the NetworkInterface.GetIsNetworkAvailable() method and populate the property properly. Need assistance? Find the code below for your reference:

 
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Net.NetworkInformation;
 
namespace NetworkAvailabilityDemo
{
    public partial class MainPage : PhoneApplicationPage
    {
        public string NetworkAvailability
        {
            get { return (string)GetValue(NetProperty); }
            set { SetValue(NetProperty, value); }
        }
 
        public static readonly DependencyProperty NetProperty =
            DependencyProperty.Register("NetworkAvailability", 
                                         typeof(string), 
                                         typeof(MainPage), 
                                         new PropertyMetadata(string.Empty));
        
        // Constructor
        public MainPage()
        {
            InitializeComponent();
 
            NetworkAvailability = NetworkInterface.GetIsNetworkAvailable() ? 
                                                "available" : "not available";
        }
    }
}

 

Now move onto the XAML page and add a TextBlock inside the ContentPanel Grid control. Bind the text properly with the DependencyProperty that we exposed. Find the code here in case you need reference:

 
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <TextBlock Text="{Binding NetworkAvailability, ElementName=phonePage, StringFormat='Internet Connection is {0}'}" FontSize="30"/>
</Grid>

 

Remember to use the ElementName attribute to the name of the class while binding. We named the XAML page as "phonePage" and hence the ElementName is set to phonePage to retrieve the proper bindings.

 

Demo

Let's see the demo of it. Run the application inside the Phone Emulator. Once the application runs, it will show that the Internet Connection is Available. Here, the API returns True and based on this, our code sets the value in the screen.

 

Find the screenshot of the running demo here:

 

WP7.1 Demo - Detecting Network Connectivity

 

Hope this gave you some basic understandings about the API. Now you will be able to use it in your application to check whether the network connectivity is available to your phone and depending upon the returned value you will be able to take action based on your business requirement. Let me know, if you have any feedback. Enjoy reading my articles. Happy Coding. Cheers.

Have a question? Or, a comment? Let's Discuss it below...

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.