检查 windows 8 phone 应用程序中是否打开了互联网?

Check if Internet is turned on in windows 8 phone app?

我有一个 Windows Phone 8 应用程序。在我的应用程序启动或打开之前,我需要检查设备是否有互联网连接。

如果互联网是 off 那么它应该将用户引导至 settings pageturn on the internet

public static bool checkNetworkConnection()
{
    var ni = NetworkInterface.NetworkInterfaceType;

    bool IsConnected = false;
    if ((ni == NetworkInterfaceType.Wireless80211)|| (ni == NetworkInterfaceType.MobileBroadbandCdma)|| (ni == NetworkInterfaceType.MobileBroadbandGsm))
        IsConnected= true;
    else if (ni == NetworkInterfaceType.None)
        IsConnected= false;
    return IsConnected;
}

我试过了,但它说:

NetworkInterfaceType not found in System.Net.NetworkInformation

有人可以指导我吗。

请尝试以下代码:

public static bool checkNetworkConnection()
{
    return NetworkInterface.GetIsNetworkAvailable();
}

使用此命名空间: using Microsoft.Phone.Net.NetworkInformation; 获取网络信息。

希望对您有所帮助。

试试下面的代码:

bool Connection = checkNetworkConnection()
if(Connection == false)
{ 
     // Here is ConnectionSettingTask to Go directly InternetSetting 
     ConnectionSettingsTask cst = new ConnectionSettingsTask();
     cst.ConnectionSettingsType = ConnectionSettingsType.Cellular;
     cst.Show(); 
}
public static bool checkNetworkConnection() 
{
    var ni = NetworkInterface.NetworkInterfaceType;
    bool IsConnected = false;
    if ((ni == NetworkInterfaceType.Wireless80211)|| (ni == NetworkInterfaceType.MobileBroadbandCdma)|| (ni == NetworkInterfaceType.MobileBroadbandGsm))
        IsConnected= true;
    else if (ni == NetworkInterfaceType.None)
         IsConnected= false;
    return IsConnected;
}