tempid
tempid

Reputation: 8198

Display error message if there's no internet connection

I have a Metro app done using C# & XAML. I'm using the WebView control to load a URL, and also using Notifications to update the tiles. If the machine isn't connected to the internet, the tile, and the webview are blank. I want to be display some sort of a message indicating that the app isn't able to connect to the Internet.

How do I check for Internet connection? In a try catch block or something?

Upvotes: 1

Views: 1099

Answers (1)

Jeff Brand
Jeff Brand

Reputation: 5633

Have you looked at the Network Information sample? It shows how to check for internet connectivity from inside your app. Short version...

var connectionProfile = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile();

switch (connectionProfile.GetNetworkConnectivityLevel())
                {
                    case NetworkConnectivityLevel.None:
                        connectionProfileInfo += "Connectivity Level : None\n";
                        break;
                    case NetworkConnectivityLevel.LocalAccess:
                        connectionProfileInfo += "Connectivity Level : Local Access\n";
                        break;
                    case NetworkConnectivityLevel.ConstrainedInternetAccess:
                        connectionProfileInfo += "Connectivity Level : Constrained Internet Access\n";
                        break;
                    case NetworkConnectivityLevel.InternetAccess:
                        connectionProfileInfo += "Connectivity Level : Internet Access\n";
                        break;
                }

Make a check before trying to use the WebView and prompt the user accordingly.

Upvotes: 2

Related Questions