Checking for Connectivity the Bulletproof Way

A huge amount of the currently released apps in the Store does a check upon app start up to see if connectivity is available. If this is not the case, then the user is redirected to an offline page, and there is no way to continue use of the app.

This is a major concern, when several of the apps does not correctly check for connectivity. One such case, is when the user connects to a VPN. In that case the main connection will be changed to ‘Limited’ even though the user still has an active connection to the internet.

The below snippet will check all the connection profiles for internet access:

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

The naive way to check for internet access would be: NetworkInformation.GetInternetConnectionProfile().GetNetworkConnectivityLevel()== NetworkConnectivityLevel.InternetAccess , but this approach fails in the scenario described above.

Remember ScrollViewer Position

Often in Windows 8 apps the user will be presented with long horizontal groups of content. After having navigated to these items, and then returning to the original page there is no built in functionality to remember where in the list that the user scrolled to.

Creating this feature is relatively quick:

  1. Whenever the user leaves the page, remember the current scroll position.
  2. Whenever the user navigates back to the page, retrieve the previous scroll position
  3. When all your content is loaded, scroll to the previous position
// Step 1
private double? horizontalOffsetState;

protected override void SaveState(Dictionary<string, object> pageState)
{
base.SaveState(pageState);

var myScrollViewer = … // your scroll viewer
var offset = myScrollViewer.HorizontalOffset;

pageState["horizontalOffset"] = offset;
}

// Step 2
protected override void LoadState(object navigationParameter,Dictionary<string, object> pageState)
{
base.LoadState(navigationParameter, pageState);

if (pageState != null)
{
horizontalOffsetState = (double) pageState["horizontalOffset"];
}
}

// step 3 - put in constructor or loadstate/navigatedTo
this.Loaded += (sender, args) =>
{
var myScrollViewer = … // your scroll viewer
if (horizontalOffsetState.HasValue)
{
myScrollViewer.ScrollToHorizontalOffset(horizontalOffsetState.Value);
}

};