문제

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?

도움이 되었습니까?

해결책

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
}

다른 팁

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);
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top