如何在不同的class中获取互联网的检查结果?

How to get the result of the check of the Internet in a different class?

在我的应用程序中检查 Internet 连接和对我网站的访问。我无法在另一个 class 中得到结果。通过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";
    }
    ...

    }
}
var gen = new NetworkUtils().GetConnectionGeneration();
if (gen == NetworkUtils.ConnType.CONN_MY) {
   //...
}

问题是您正在检查枚举成员 (CONN_MY) 的 toString,因此您的 if 语句的第一行始终为真,并且它将始终显示 Good Connection。您必须先致电 GetConnectionGeneration()

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
}