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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.