2016-10-01 41 views
-2

在我的应用程序中检查Internet连接并访问我的网站。我无法在另一个班上得到结果。通过localSettings.Values有一个解决方案,但它不是我想要的。请帮帮我。谢谢如何在不同的课程中获得互联网的检查结果?

public class NetworkUtils{  

    public enum ConnType 
       { 
        CONN_MOBILE, 
        CONN_WIFI, 
        CONN_WAN, 
        CONN_NO, 
        CONN_MY 
       } 

      public ConnType GetConnectionGeneration() 
       { 
        string connectionProfileInfo = string.Empty; 
        try 
        { 
         ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile(); 

         if (InternetConnectionProfile == null) 
         { 
          NotifyUser("Not connected to Internet\n"); 
          return (ConnType.CONN_NO); 
         } 
         else 
         { 
          if (InternetConnectionProfile.IsWlanConnectionProfile) 
          { 
           HttpClient httpClient = new HttpClient(); 
           httpClient.Timeout = new TimeSpan(0, 0, 0, 0, 300); 
           try 
           { 
            httpClient.GetAsync(new Uri("http://www.myexample.com")).Wait(); 
            NotifyUser("MY SITE connected\n"); 
            return (ConnType.CONN_MY); 
           } 
           catch (Exception ex) 
           { 
            NotifyUser("Unexpected exception occurred: " + ex.ToString()); 
           } 
           return (ConnType.CONN_WIFI); 
          } 
          else if (InternetConnectionProfile.IsWwanConnectionProfile) 
          { 
           WwanDataClass connectionClass = InternetConnectionProfile.WwanConnectionProfileDetails.GetCurrentDataClass(); 
           switch (connectionClass) 
           { 
            //2G-equivalent 
            case WwanDataClass.Edge: 
            case WwanDataClass.Gprs: 
            //3G-equivalent 
            case WwanDataClass.Cdma1xEvdo: 
            case WwanDataClass.Cdma1xEvdoRevA: 
            case WwanDataClass.Cdma1xEvdoRevB: 
            case WwanDataClass.Cdma1xEvdv: 
            case WwanDataClass.Cdma1xRtt: 
            case WwanDataClass.Cdma3xRtt: 
            case WwanDataClass.CdmaUmb: 
            case WwanDataClass.Umts: 
            case WwanDataClass.Hsdpa: 
            case WwanDataClass.Hsupa: 
            //4G-equivalent 
            case WwanDataClass.LteAdvanced: 
             return (ConnType.CONN_MOBILE); 

            //not connected 
            case WwanDataClass.None: 
             return (ConnType.CONN_NO); 

            //unknown 
            case WwanDataClass.Custom: 
            default: 
             HttpClient httpClient = new HttpClient(); 
             httpClient.Timeout = new TimeSpan(0, 0, 0, 0, 300); 
             try 
             { 
              httpClient.GetAsync(new Uri("http://www.myexample.com")).Wait(); 
              return (ConnType.CONN_MY); 
             } 
             catch (Exception ex) 
             { 
              NotifyUser("Unexpected exception occurred: " + ex.ToString()); 
             } 
             return (ConnType.CONN_WAN); 
           } 
          } 
          return (ConnType.CONN_MOBILE); 
         } 
        } 
        catch (Exception ex) 
        { 
         NotifyUser("Unexpected exception occurred: " + ex.ToString()); 
         return (ConnType.CONN_NO); 
        } 
       } 
    } 

如果我做的事情不那么正确。如果有好的想法,我也会很感激。我测试过,但没有任何反应。

public sealed partial class TestPage : Page 
     { 
    public TestPage() 
      { 
    InitializeComponent(); 
    if (NetworkUtils.ConnType.CONN_MY.ToString().Equals("CONN_MY"){ 
    TextBlock.Text = "GOOD Connection"; 
    } else if (NetworkUtils.ConnType.CONN_WIFI.ToString().Equals("CONN_MY"){ 
    TextBlock.Text = "WIFI Connection"; 
    } 
    ... 

    } 
} 

回答

0
var generation = new NetworkUtils().GetConnectionGeneration(); 
swtich (generation) 
{ 
    case NetworkUtils.ConnType.CONN_MY: 
    { 
     //... 
     break; 
    } 
    case NetworkUtils.ConnType.CONN_WIF: 
    { 
     //... 
     break; 
    } 

    // othes cases 

    default: 
    // handle exceptional case 
} 
0
var gen = new NetworkUtils().GetConnectionGeneration(); 
if (gen == NetworkUtils.ConnType.CONN_MY) { 
    //... 
} 

问题是要检查枚举成员(CONN_MY)的toString,所以你的if语句始终是真实的,它会始终显示Good Connection第一线。您必须先致电GetConnectionGeneration()