UWP NetworkConnectionChanged 事件 [英] UWP NetworkConnectionChanged Event

查看:17
本文介绍了UWP NetworkConnectionChanged 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 UWP 应用,需要在网络连接丢失或设备重新连接后做一些事情.

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?

我搜索了 www,但我发现的任何内容都是针对 WP8 的...

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

我需要这个用于 Windows 10 上的 UWP.

I need this for UWP on Windows 10.

我尝试使用 NetworkInformation.NetworkStatusChanged .

推荐答案

我也遇到了同样的问题.这篇文章(以及整个网站)对我帮助很大:http://windowsapptutorials.com/windows-10/how-to-check-for-network-availability-in-universal-windows-apps/

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/

我希望这是你需要的.问题是,您只能检测例如 wifi 或移动网络是否已连接.如果您已连接 wifi 并拔下路由器上的插头,则它不会检测到它.

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.

现在我是这样做的:

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

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

    return isInternetConnected;
}

我相信这种方法在模拟器中不起作用,但我不确定.(目前正在重新安装 VS15,所以我无法再次测试).

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

希望能帮到你.

这篇关于UWP NetworkConnectionChanged 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆