loo*_*oop 4 .net c# windows-phone-8 windows-phone-8.1 windows-phone-sl-8.1
我想检查一下我的手机是否可以连接到互联网.我已经看过几个问题.其中一个是问题.它说使用NetworkInterface.GetIsNetworkAvailable()这个,我尝试了.我已将我的电脑与互联网断开连接,并且还关闭DataConnection了模拟器,但这NetworkInterface.GetIsNetworkAvailable()始终是真的.但同时我也检查NetworkInterfaceType.None并有趣地它将变为空.任何人都可以解释我在哪里缺少信息?
尝试: -
public static void CheckNetworkAvailability()
{
// this is coming true even when i disconnected my pc from internet.
// i also make the dataconnection off of the emulator
var fg = NetworkInterface.GetIsNetworkAvailable();
var ni = NetworkInterface.NetworkInterfaceType;
// this part is coming none
if (ni == NetworkInterfaceType.None)
IsConnected = false;
}
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏:)
我正在使用以下代码检查设备是否可以访问互联网,如果它连接到wifi或数据连接..
public void UpdateNetworkInformation()
{
// Get current Internet Connection Profile.
ConnectionProfile internetConnectionProfile = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile();
//air plan mode is on...
if (internetConnectionProfile == null)
{
Is_Connected = false;
return;
}
//if true, internet is accessible.
this.Is_InternetAvailable = internetConnectionProfile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
// Check the connection details.
else if (internetConnectionProfile.NetworkAdapter.IanaInterfaceType != 71)// Connection is not a Wi-Fi connection.
{
Is_Roaming = internetConnectionProfile.GetConnectionCost().Roaming;
/// user is Low on Data package only send low data.
Is_LowOnData = internetConnectionProfile.GetConnectionCost().ApproachingDataLimit;
//User is over limit do not send data
Is_OverDataLimit = internetConnectionProfile.GetConnectionCost().OverDataLimit;
}
else //Connection is a Wi-Fi connection. Data restrictions are not necessary.
{
Is_Wifi_Connected = true;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑: 对于简单的互联网连接,您可以使用下面的代码.
System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
NetworkInterface.GetIsNetworkAvailable()即使您将网络条件模拟为没有网络,模拟器也始终返回true.
我自己遇到了这个问题,测试此行为的唯一真正方法是将应用程序部署到运行Windows Phone的物理设备,并在关闭数据的情况下对其进行测试.
| 归档时间: |
|
| 查看次数: |
4559 次 |
| 最近记录: |