3

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

如果是 Internet,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

有人可以指导我吗?

4

2 回答 2

1

请尝试以下代码:

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

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

希望这对你有帮助。

于 2015-10-21T04:46:57.487 回答
1

试试下面的代码:

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;
}
于 2015-10-21T05:59:20.690 回答