使用bing map api处理Windows 8应用程序中没有互联网连接

时间:2014-01-17 11:03:37

标签: c# windows-8.1 bing-maps internet-connection

我使用本指南http://www.codeproject.com/Articles/408457/Using-Bing-Maps-For-Windows-8-Metro-Apps-Csharp-Ja在C#中使用Bing map sdk创建Windows 8.1应用。

现在当我使用这个没有互联网连接的应用程序时,应用程序崩溃了。那么我该如何处理这个没有互联网连接的例外呢?

3 个答案:

答案 0 :(得分:2)

将此属性放在App.xaml.cs中,用于测试互联网是否可用

public static bool IsInternetAvailable
{
    get
    {
        var profiles = NetworkInformation.GetConnectionProfiles();
        var internetProfile = NetworkInformation.GetInternetConnectionProfile();
        return profiles.Any(s => s.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess)
            || (internetProfile != null
                    && internetProfile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess);
    }
}

if(App.IsInternetAvailable)
{
    //Do operation of Bing map
}
else
{
    //Show message dialog
}

答案 1 :(得分:0)

先检查一下?

例如通过方法:

public static bool CheckForInternetConnection()
{
    try
    {
        using (var client = new WebClient())
        using (var stream = client.OpenRead("http://www.bing.com"))
        {
            return true;
        }
    }
    catch
    {
        return false;
    }
}

答案 2 :(得分:0)

这是我的Windows 8.1代码

public static bool have_net()
    {
        ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();

        if (InternetConnectionProfile == null) return false;

        NetworkConnectivityLevel ncl = InternetConnectionProfile.GetNetworkConnectivityLevel();

        if (ncl == NetworkConnectivityLevel.InternetAccess)
        {
            return true;
        }

        bool b1 = InternetConnectionProfile.IsWwanConnectionProfile;
        bool b2 = InternetConnectionProfile.IsWlanConnectionProfile;

        return (b1 || b2);
    }