我有一个Windows Phone 8
申请。我需要在启动或打开应用程序之前检查设备是否具有Internet连接。
如果互联网为off
,则应将用户定向到settings page
至turn 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
有人可以指导我这个。
答案 0 :(得分:1)
请尝试以下代码:
public static bool checkNetworkConnection()
{
return NetworkInterface.GetIsNetworkAvailable();
}
使用此命名空间: using Microsoft.Phone.Net.NetworkInformation;
获取网络信息。
希望这对你有所帮助。
答案 1 :(得分: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;
}