Creating a service for fetching the weather data

To make it easier to change the external weather service and to make the code more testable, we will create an interface for the service. Here's how we go about it:

  1. In the Weather project, create a new folder and name it Services.
  2. Create a new public interface and name it IWeatherService.
  3. Add a method for fetching data based on the location of the user, as shown in the following code. Name the method GetForecast:
 public interface IWeatherService {      Task<Forecast> GetForecast(double latitude, double longitude); }

When we have an interface, we can create an implementation for it by going through the following steps:

  1. In the Services folder, create a new class with the name OpenWeatherMapWeatherService ...

Get Xamarin.Forms Projects now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.