UWP 上是否有相当于 DeviceNetworkInformation.NetworkAvailabilityChanged 的事件?

Is there an equivalent of DeviceNetworkInformation.NetworkAvailabilityChanged event on UWP?

我正在将我的 Windows Phone 8 Silverlight 转换为 UWP,但我在 UWP

中找不到与 DeviceNetworkInformation.NetworkAvailabilityChanged 事件等效的事件

我知道在 UWP 上我们必须使用 ConnectionProfile 来获取有关用户连接(Wifi、3G 等...)的信息

ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();

但是在 ConnectionProfile 对象中似乎没有检查 Internet 是否不可用的事件。

有人知道如何在 UWP 中执行此操作吗?

谢谢

可能没有 DeviceNetworkInformation.NetworkAvailabilityChanged Event in UWP APIs by now. But we can achieve this by combining NetworkInformation.NetworkStatusChanged event with ConnectionProfile.GetNetworkConnectivityLevel method 的等价物。

Ref Remarks in ConnectionProfile.GetNetworkConnectivityLevel:

The recommended process for determining the network connectivity level is to register a handler for the NetworkStatusChanged event on the NetworkInformation class. When a notification is received of a network status change, obtain the new connectivity level by calling the GetNetworkConnectivityLevel method on the profile returned by the GetInternetConnectionProfile method. The returned network connectivity level can then be stored for later use when needed. This also ensures that the correct ConnectionProfile is checked.

以下是一个简单示例:

NetworkInformation.NetworkStatusChanged += (s) =>
{
    var profile = NetworkInformation.GetInternetConnectionProfile();
    var isInternetConnected = profile != null && profile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
};

您也可以将其封装到一个事件中,如本博客中所示:How to react to network availability changes in Windows Store apps