MOBILE

Windows Phone 7 Development : Revising WeatherRx to Manage Slow Data Connections

4/23/2011 3:59:59 PM
To modify the WeatherRx application, you will first add buttons to the UI to allow the user to either retry the failed connection or to exit gracefully. Then, you will add code to the application to react to the events on these new User Interface elements.

To add the new elements to the WeatherRx UI, do the following:

  1. pen MainPage.xaml and add two buttons right below the lblStatus textblock, as shown in Figure 1. Name the first button btnRetry and set its Content property to "Retry." Name the second button btnQuit and set its Content property to "Quit." Set the Visibility of both buttons to Collapsed.

    Figure 1. Weather application with error recovery elements

On retry, you will recreate the Observable connection to the weather web service, if it's needed, and then invoke the web service again.

  1. Double-click the "Retry" button and add the following handler code to the btnRetry_Click function:

    private void btnRetry_Click(object sender, RoutedEventArgs e)
    {
    btnQuit.Visibility = System.Windows.Visibility.Collapsed;
    btnRetry.Visibility = System.Windows.Visibility.Collapsed;
    lblStatus.Text = "";

    WireUpWeatherEvents();
    weatherClient.GetWeatherByZipCodeAsync(txtZipCode.Text);
    }

If the user selects Quit, let's simply hide the buttons and the exception text.

  1. Double-click the "Quit" button and add the following code to the btnQuit_Click function:

    private void btnQuit_Click(object sender, RoutedEventArgs e)
    {
    btnQuit.Visibility = System.Windows.Visibility.Collapsed;
    btnRetry.Visibility = System.Windows.Visibility.Collapsed;
    lblStatus.Text = "";

    }

Finally, you will need to ensure there is only one subscription to the weather web service at any given time.

  1. To ensure that there's only one subscription, first declare a module level variable by adding the following statement:

    IObservable<IEvent<GetWeatherByZipCodeCompletedEventArgs>> _weather;

  2. Next, change the GetWeatherSubject function to the following:

    private void GetWeatherSubject()
    {
    if (_weather == null)
    {
    _weather = Observable.FromEvent<svcWeather.GetWeatherByZipCodeCompletedEventArgs>(weatherClient, "GetWeatherByZipCodeCompleted");
    }
    }


  3. Finally, change the WireUpWeatherEvents method to look like the following (notice how the timeout value is now set to a more reasonable five seconds as well):

    private void WireUpWeatherEvents()
    {
    GetWeatherSubject();
    _weather.ObserveOn(Deployment.Current.Dispatcher)
    .Timeout(TimeSpan.FromSeconds(5))
    .Subscribe(evt =>
    {
    if (evt.EventArgs.Result.Details != null)
    {
    lblWeatherFahrenheit.Text = "Current Weather, Fahrenheit: " + evt.EventArgs.Result.Details[0].MinTemperatureF.ToString() + " - " + evt.EventArgs.Result.Details[0].MaxTemperatureF.ToString();
    lblCelsius.Text = "Current Weather, Celsius: " + evt.EventArgs.Result.Details[0].MinTemperatureC.ToString() + " - " + evt.EventArgs.Result.Details[0].MaxTemperatureC.ToString();
    imgWeather.Source = new BitmapImage(new Uri(evt.EventArgs.Result.Details[0].WeatherImage, UriKind.Absolute));
    }
    },
    ex =>
    {
    Deployment.Current.Dispatcher.BeginInvoke(() => lblStatus.Text = ex.Message);
    Deployment.Current.Dispatcher.BeginInvoke(() =>
    btnQuit.Visibility=System.Windows.Visibility.Visible);
    Deployment.Current.Dispatcher.BeginInvoke(() => btnRetry.Visibility =
    System.Windows.Visibility.Visible);
    }
    );
    }


This walkthrough illustrates one approach to handling connection issues on Windows Phone 7 devices: you specify a timeout period, and if you don't get a response within that period, you prompt the user to retry or to quit.

Other  
  •  Windows Phone 7 Development : Handling Data Connection Issues with Rx.NET
  •  Windows Phone 7 Development : Handling Errors in Rx.NET
  •  Windows Phone 7 Development : Using Rx.NET with Web Services to Asynchronously Retrieve Weather Data
  •  Windows Phone 7 Development : Media - Adding Sounds to an Application
  •  iPhone Application Development : Building a Multi-View Tab Bar Application (part 4) - Implementing the Summary View
  •  iPhone Application Development : Building a Multi-View Tab Bar Application (part 3) - Implementing the Volume View
  •  iPhone Application Development : Building a Multi-View Tab Bar Application (part 2) - Implementing the Area View
  •  iPhone Application Development : Building a Multi-View Tab Bar Application (part 1)
  •  Windows Phone 7 Development : Working with Video (part 2) - Coding the Application
  •  Windows Phone 7 Development : Working with Video (part 1)
  •  Windows Phone 7 Development : Plotting an Address on a Bing Maps Map and Working with the Bing Maps Service
  •  Windows Phone 7 Development : Using GeoCoordinateWatcher and the Bing Maps Control to Track Your Movements
  •  iPhone Application Development : Creating a Multi-View Toolbar Application (part 3) - Adding Toolbar Controls
  •  iPhone Application Development : Creating a Multi-View Toolbar Application (part 2) - Instantiating the View Controllers
  •  iPhone Application Development : Creating a Multi-View Toolbar Application (part 1)
  •  Windows Phone 7 Development : Using Location Services - Simulating the Location Service
  •  Introducing the Windows Phone Location Service and Mapping APIs
  •  iPhone Application Development : Implementing a Custom Picker View (part 4) - Tweaking the Picker UI
  •  iPhone Application Development : Implementing a Custom Picker View (part 3) - Reacting to a Picker View Choice
  •  iPhone Application Development : Implementing a Custom Picker View (part 2)
  •  
    Video
    Top 10
    How To Create A Window Light Effect And Much More (Part 2)
    How To Create A Window Light Effect And Much More (Part 1)
    Huawei Ascend G300 On Vodafone
    SpeedUpMyPC 2012
    PNY Professional SSD
    Plextor M3 Pro 256GB
    Patriot Pyro SE
    Kingston HyperX
    Google Drive
    CyberLink MediaSuite 10 Ultra Review
    Most View
    SQL Server 2008 : T-SQL Tips and Tricks (part 1) - Date Calculations & Sorting Results with the GROUPING Function
    Windows Server 2008: Improvements for Thin Client Remote Desktop Services
    Programming .NET Security : Programming Asymmetrical Encryption
    The Assembly/Namespace/Type Distinction
    Windows Server 2003 : Managing User Profiles
    jQuery 1.3 : Table Manipulation - Sorting and paging (part 1) : Server-side sorting & JavaScript sorting
    IIS 7.0 : Implementing Security Strategies - Configuring Applications for Least Privilege
    iPhone 3D Programming : Adding Textures to ModelViewer (part 4) - Enabling Textures with ES2::RenderingEngine
    SQL Server 2005 : Implementing Service Broker
    The key to security
    AC Ryan Playon! HD2 Mini
    Choosing a super-zoom camera (part 2)
    Changes in Windows Vista Affecting SDI
    iPhone Application Development : Building a Multi-View Tab Bar Application (part 1)
    SQL Server 2008 : Working with DML Queries - Using the INSERT Statement (part 2)
    Predict The Weather (Part 2) - Feathery storm clouds
    IIS 7.0 : Managing Configuration - Backing Up Configuration, Using Configuration History & Exporting and Importing Configuration
    MediaElement in Silverlight
    Mobile Commerce Applications, Part 2
    Windows Server : Designing a Software Update Infrastructure (part 1)