且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Windows Phone 8 应用程序中检查互联网连接的可用性

更新时间:2023-01-01 18:41:01

NetworkInterface.GetIsNetworkAvailable() 返回 NIC 的状态.

NetworkInterface.GetIsNetworkAvailable() returns the status of the NICs.

根据状态,您可以使用以下方法询问是否已建立连接:

Depending on the status you can ask if the connectivity is established by using:

ConnectionProfile-使用 enum NetworkConnectivityLevel 的 Windows Phone 8.1 类:

ConnectionProfile-Class of Windows Phone 8.1 which uses the enum NetworkConnectivityLevel:

  • 本地访问
  • 互联网接入

这段代码应该可以解决问题.

This code should do the trick.

bool isConnected = NetworkInterface.GetIsNetworkAvailable();
if (isConnected)
{
    ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
    NetworkConnectivityLevel connection = InternetConnectionProfile.GetNetworkConnectivityLevel();
    if (connection == NetworkConnectivityLevel.None || connection == NetworkConnectivityLevel.LocalAccess)
    {
        isConnected = false;
    }
}
if(!isConnected)
    await new MessageDialog("No internet connection is avaliable. The full functionality of the app isn't avaliable.").ShowAsync();