Domanda

I am creating a Windows 8.1 app using Bing map sdk in C# using this guide http://www.codeproject.com/Articles/408457/Using-Bing-Maps-For-Windows-8-Metro-Apps-Csharp-Ja.

Now when I am using this app without internet connection the app crashes. So how can I deal with this no internet connection exception?

È stato utile?

Soluzione

Put this property in App.xaml.cs as use it to test if Internet is available or not

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
}

Altri suggerimenti

Check it first?

For example by method:

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

Here is my code for 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);
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top