如何在 Windows Phone 8 应用程序中检查互联网连接的可用性 [英] How to check the internet connection availability in windows phone 8 application

查看:23
本文介绍了如何在 Windows Phone 8 应用程序中检查互联网连接的可用性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 Windows Phone 8 应用程序.在这个应用程序中,我必须连接到服务器才能获取数据.

I'm developing Windows Phone 8 application. In this application, I have to connect to the server to get the data.

所以在连接到服务器之前,我想检查设备是否可以使用互联网连接.如果互联网连接可用,那么我只会从服务器获取数据,否则我会显示错误消息.

请告诉我如何在 Windows Phone 8 中执行此操作.

Please tell me how to do this in Windows Phone 8.

推荐答案

NetworkInterface.GetIsNetworkAvailable() 返回 NIC 的状态.

NetworkInterface.GetIsNetworkAvailable() returns the status of the NICs.

根据状态,您可以使用以下方法询问是否已建立连接:

Depending on the status you can ask if the connectivity is established by using:

ConnectionProfile-使用 enum NetworkConnectivityLevel 的 Windows Phone 8.1 类:

ConnectionProfile-Class of Windows Phone 8.1 which uses the enum NetworkConnectivityLevel:

  • 本地访问
  • 互联网接入

这段代码应该可以解决问题.

This code should do the trick.

bool isConnected = NetworkInterface.GetIsNetworkAvailable();
if (isConnected)
{
    ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
    NetworkConnectivityLevel connection = InternetConnectionProfile.GetNetworkConnectivityLevel();
    if (connection == NetworkConnectivityLevel.None || connection == NetworkConnectivityLevel.LocalAccess)
    {
        isConnected = false;
    }
}
if(!isConnected)
    await new MessageDialog("No internet connection is avaliable. The full functionality of the app isn't avaliable.").ShowAsync();

这篇关于如何在 Windows Phone 8 应用程序中检查互联网连接的可用性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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