12

My team is working on a team project aplication. At the moment, we need an event handler to check the connection status (if it's on/off).

I had big hopes in the System.Net.NetworkInformation Namespace, but unfortunately most important things aren't supported in wp8.

Can anyone help me with it a bit?

Edit 1#

It seems, I didn't specifed my problem well. I'm using Mvvm light expresion, and it does not support that namespace or at least I can't add it.

I'm a newbie in using VS and c# atm, mayby I'm doing someting wrong, but simply when im trying to add the refernce to my project it does not list.

Sina
  • 607
  • 7
  • 22
Przmak
  • 133
  • 1
  • 1
  • 7

3 Answers3

29

I haven't tried the System.Net.NetworkInformation namespace on WP8. But the new WP8 Windows.Networking.Connectivity Windows Phone Runtime namespace works just fine.

Use Windows.Networking.Connectivity.NetworkInformation.NetworkStatusChanged to know when network conditions change and use Microsoft.Phone.Net.NetworkInformation.NetworkInterface properties or Windows.Networking.Connectivity.NetworkInformation properties to see what's up.

    private async void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        PrintNetworkStatus();

        NetworkInformation.NetworkStatusChanged += NetworkInformation_NetworkStatusChanged;
    }

    void NetworkInformation_NetworkStatusChanged(object sender)
    {
        PrintNetworkStatus();
    }

    private void PrintNetworkStatus()
    {
        Dispatcher.BeginInvoke(() =>
        MessageBox.Show(NetworkInterface.NetworkInterfaceType +
                        Environment.NewLine +
                        NetworkInterface.GetIsNetworkAvailable()));
    }

When I test this code snippet on my WP8 Lumia 920 it works as expected. On startup when my phone is on WiFi only I see the following MessageBox:

Connected Messagebox

And once I shutdown my WiFI router and the WiFi connection on the phone is lost I see the following MessageBox:

Disconnected MessageBox

JustinAngel
  • 16,082
  • 3
  • 44
  • 73
  • I tried that, It works on WP8 project but doesn't work on Mvvm light projects – Przmak Nov 29 '12 at 10:44
  • 1
    I'm unclear what an "MVVM Light project" is. AFAIK it's just a derivation of the original project template. Maybe it's best if you start from a WP8 New Project and add whatever 3rd party components and architecture you need to that project. – JustinAngel Nov 29 '12 at 16:29
18

Try this:

bool isNetwork=NetworkInterface.GetIsNetworkAvailable();
if(!isNetwork)
{
   //proceed with your code

}
asteri
  • 11,402
  • 13
  • 60
  • 84
Apoorv
  • 2,023
  • 1
  • 19
  • 42
2

In App.xaml.cs, create a property like below

/// <summary>
/// check if network is available
/// </summary>
public bool IsNetworkAvailable
{
    get
    {
        return NetworkInterface.NetworkInterfaceType != NetworkInterfaceType.None;
    }
}

And you can use this property anywhere in your project as in below code

if (((App) Application.Current).IsNetworkAvailable)
{
         //Lines of Code
}
else
{
     MessageBox.Show("Not Connected to Network!", "Checking Connection!",
               MessageBoxButton.OK);     
}
Pravesh Pesswani
  • 481
  • 8
  • 16