In MVVM, the ViewModel is the place to handle the logic of the app. The model is the news data that we will get from our NewsService class. We will now extend the HeadlinesViewModel class to use the NewsService to fetch news by following these two steps:
- In the News project, expand the ViewModels folder and open the HeadlinesViewModel.cs file.
- Add the following code marked in bold and resolve references:
using System;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Input;
using News.Models;
using News.Services;
using Xamarin.Forms;
namespace News.ViewModels
{
public class HeadlinesViewModel : ViewModel
{
private readonly NewsService newsService;
public NewsResult CurrentNews { get; set; }
public HeadlinesViewModel(NewsService newsService)
{
this.newsService = newsService;
}
public async Task Initialize(string scope)
{
var resolvedScope = scope.ToLower() switch
{
"local"...