简体   繁体   English

Windows Phone 8.1模拟器检查Internet连接

[英]Windows Phone 8.1 emulator check Internet connection

I need to test my app in emulator, check Internet connection. 我需要在模拟器中测试我的应用程序,检查Internet连接。 I am using following class (from here http://www.jayway.com/2015/04/23/how-to-verify-internet-access-in-universal-apps/ ): 我正在使用以下课程(来自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);
    }
}

and checking connection by using class: 并使用类检查连接:

Connection conn = new Connection();
if (Connection.HasInternetAccess) {
      // some code
} else {
      MessageDialog msgbox = new MessageDialog("no internet");
      await msgbox.ShowAsync();
}

Problem is I get true every time, in emulator. 问题是我每次都会在模拟器中变为现实。 I tried solution from here How to disconnect Windows Phone 8.1 emulator from network? 我试过这里的解决方案如何断开Windows Phone 8.1模拟器与网络的连接? , even rebooted PC, but nothing changed. ,甚至重新启动PC,但没有任何改变。

Testing on the device was correct. 在设备上进行测试是正确的。 True if connection enabled, false if connection disabled. 如果启用连接则为True,如果禁用连接则为false。

PS Sorry for my terrible English. PS对不起我糟糕的英语。

The emulator always returns NetworkInformation.GetInternetConnectionProfile() as true, even if you emulate network conditions as having no network. 即使您将网络条件模拟为没有网络,模拟器也始终将NetworkInformation.GetInternetConnectionProfile()返回true。

Use physical device to test the app.(it will works fine). 使用物理设备测试应用程序。(它会正常工作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM