Blog

All Blog Posts

WP7 Insights #4: Detecting and Reacting to Connectivity

This article is the fourth in a series which discusses some of the things that I learned while developing my first Windows Phone 7 application.

Update—I now have two apps in the WP7 marketplace, so I have more insights to share.

My latest WP7 app displays events for our community's annual summer celebration, Sturgis Falls. This app displays a schedule which can be viewed by venue, day, or category. Displaying the events and other data relies on an active connection with your phone.

One mistake I made which was caught during the Microsoft Certification process was that I did not handle scenarios which require a data connection but no connection was available. This resulted in a certification failure. #Fail

Fortunately it is pretty easy to detect and react to network servicing Internet requests.  Start by using the NetworkInterface Class, which is in Microsoft.Phone.dll. You can use this class to check the network type and detect when the network type is changed. In my app I only needed to check the network type when my page loaded since that is when I utilize the network connection.

The following code sample runs when a user selects a Venue from a list of Venues. This page will load all events for the selected Venue. If there is no network connection then I disable the progress bar and provide feedback to the user. Note—Instead of changing the TextBlock to say "No Connection" I could have called MessageBox.Show() to pop a message to the user.

     void VenueDetails_Loaded(object sender, RoutedEventArgs e)
  {
      if (NetworkInterface.NetworkInterfaceType == NetworkInterfaceType.None)
      {
          myProgress.Visibility = Visibility.Collapsed;
          PageTitle.Text = "No Connection";
      }
      else
      {
          _viewModel.LoadEventsByVenueId(_venueId);
          PageTitle.Text = _viewModel.GetVenueNameByVenueId(_venueId);
      }
  }

One other tip related to connectivity relates to my previous post about activating and deactivating. When my app closes or deactivates I am saving the events, venues, and other data to Isolated Storage. When my app starts or reactivates I can check the network type to determine if I should load data from Isolated Storage or from the web service. The following code demonstrates this.

     private void Application_Activated(object sender, ActivatedEventArgs e)
  {
      LoadSettings();
  }
     void LoadSettings()
  {
      IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
      if (NetworkInterface.NetworkInterfaceType == NetworkInterfaceType.None)
      {
          if (settings.Contains("gEvents"))
              Helpers.Globals.gEvents = (List<SiteLightEvent>)settings["gEvents"];
          if (settings.Contains("gVenues"))
              Helpers.Globals.gVenues = (List<SiteLightVenue>)settings["gVenues"];
      }
  }

 By implementing these techniques I am able to provide a user a better experience by:
  • Using data from my previous session if no network is available
  • Notify users when no data connection is available
  • Not locking up any screens when there is no data connection and therefore allowing my app to pass Microsoft Certification Testing.
I hope you find these tips useful. 

Thanks!