UWP NetworkConnectionChanged Event

半腔热情 提交于 2019-12-20 02:45:20

问题


I am developing a UWP App and need to do some things after I lost network connection or the device connected again.

Is there any event firing after a connection is lost or connected?

I searched the www but anything I found was for WP8...

I need this for UWP on Windows 10.

I tried to use NetworkInformation.NetworkStatusChanged .


回答1:


I had the same problem too. This article (and the whole website) has helped me alot: http://windowsapptutorials.com/windows-10/how-to-check-for-network-availability-in-universal-windows-apps/

I hope It's what you need. The Problem is, that you can only detect if for example the wifi or mobile network is connected. If you are connected with your wifi and pull the plug on the router it won't detect it.

Now I do it like this:

public static bool IsInternetConnected() {
    var isInternetConnected = false;
    var connectionProfile = NetworkInformation.GetInternetConnectionProfile();

    if (connectionProfile != null) {
        var connectivityLevel = connectionProfile.GetNetworkConnectivityLevel();
            isInternetConnected = connectivityLevel == NetworkConnectivityLevel.InternetAccess;
    }

    return isInternetConnected;
}

I believe this method doesn't work in the emulator but I'm not sure. (Currently reinstalling VS15 so I cant test it again).

Hopefully I could help you.



来源:https://stackoverflow.com/questions/33144533/uwp-networkconnectionchanged-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!