问题
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