WP7 check if internet is available

My WP7 application was not accepted because it does not load if the Internet is not available. I was looking for a way to test it and found this command

NetworkInterface.GetIsNetworkAvailable()

But it does not work on the emulator, and I do not have any device to check it. Can someone tell me if it returns false if the device is in airplane mode? If not, how can I check it?

Thanks Oscar

Edit: I also tried using this code:

try
{
    wsClient.CurrenciesCompleted += new EventHandler<CurrencyConversion.CurrenciesCompletedEventArgs>(wsClient_CurrenciesCompleted);
    wsClient.CurrenciesAsync(null);
}
catch
{
     NetworkNotAvailable();
}

But I can not catch the exception, I also tried in the wsClient_CurrenciesCompleted method, but also not good.

Where can I check it out?

+5
source share
4 answers

" " - , . , - , . , , :

  • , , .
  • , , .
+6

Jon - , . , , , . , , Enum .

public class NetworkMonitorClass 
{
   private Timer timer;
   private NetworkInterfaceType _currNetType = null; 
   private volatile bool _valueRetrieved = false;

   public NetworkMonitorClass()
   {
       //using a timer to poll the network type.
       timer = new Timer(new TimerCallBack((o)=>
       {
           //Copied comment from Microsoft Example:
           //  Checking the network type is not instantaneous
           //  so it is advised to always do it on a background thread.
           _currNetType = Microsoft.Phone.Net.NetworkInformation.NetworkInterface.NetworkInterfaceType;
           _valueRetrieved = true;
       }), null, 200, 3000); // update the network type every 3 seconds.
   }

   public NetworkInterfaceType CurrentNetworkType 
   { 
       get
       {  
           if(false == _valueRetrieved ) return NetworkInterfaceType.Unknown;  
           return _currNetType; 
       } 
       private set { ;} 
   }

   public bool isNetworkReady()
   {
       if(false == _valueRetrieved ) return false;

       switch (_currentNetworkType)
       {
          //Low speed networks
          case NetworkInterfaceType.MobileBroadbandCdma:
          case NetworkInterfaceType.MobileBroadbandGsm:
            return true;
          //High speed networks
          case NetworkInterfaceType.Wireless80211:
          case NetworkInterfaceType.Ethernet:
            return true;
          //No Network
          case NetworkInterfaceType.None:
          default:
             return false;
      } 
   }
}

. http://msdn.microsoft.com/en-us/library/microsoft.phone.net.networkinformation.networkinterface.networkinterfacetype(VS.92).aspx

+3

GetIsNetworkAvailable() true . .

, ( ) .

WebException .

private static void DownloadInfoCallback(IAsyncResult asynchronousResult)
{
    try
    {
        var webRequest = (HttpWebRequest)asynchronousResult.AsyncState;

        // This will cause an error if the request failed
        var webResponse = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);

        .....
    }
    catch (WebException exc)
    {
        // Handle error here
    }
}
+2

GetIsNetworkAvailable() .

Microsoft.Devices.Environment.DeviceType.

, , , .

0

All Articles