我需要在模拟器中测试我的应用程序,并检查网络连接。 我正在使用以下类(来自这里 http://www.jayway.com/2015/04/23/how-to-verify-internet-access-in-universal-apps/):
public class Connection
{
public static bool HasInternetAccess { get; private set; }
public Connection() {
NetworkInformation.NetworkStatusChanged += NetworkInformationOnNetworkStatusChanged;
CheckInternetAccess();
}
private void NetworkInformationOnNetworkStatusChanged(object sender) {
CheckInternetAccess();
}
private void CheckInternetAccess() {
var connectionProfile = NetworkInformation.GetInternetConnectionProfile();
HasInternetAccess = (connectionProfile != null &&
connectionProfile.GetNetworkConnectivityLevel() ==
NetworkConnectivityLevel.InternetAccess);
}
}
使用类检查连接:
Connection conn = new Connection();
if (Connection.HasInternetAccess) {
// some code
} else {
MessageDialog msgbox = new MessageDialog("no internet");
await msgbox.ShowAsync();
}
问题是在模拟器中每次都返回true。我尝试了这里的解决方案(如何断开Windows Phone 8.1模拟器与网络的连接?),甚至重新启动了电脑,但仍没有改变。
在设备上测试是正确的。如果连接已启用,则为true;如果连接已禁用,则为false。
P.S. 对不起,我的英语很糟糕。