Updating the WalksMainPageViewModel using C#

Now, we have updated our WalksMainPage code-behind file to handle deletions from the SQL Server database. Our next step is to start implementing the necessary code within the WalksMainPageViewModel class, which will be used by our WalksMainPage.

The WalksMainPageViewModel ViewModel class will be used to populate our data model from our SQL Server database by calling the GetWalkEntries on our AzureDatabase property and displaying the information within our ListView by setting the BindingContext within the ContentPage.

Let's take a look at how we can achieve this by going through the following steps:

  1. Locate and open the WalksMainPageViewModel.cs file, which is located within the ViewModels folder, and ensure that it is displayed within the code editor. Enter the following highlighted code sections:
    //
// WalksMainPageViewModel.cs
// The ViewModel for our WalksMainPage ContentPage
//
// Created by Steven F. Daniel on 5/06/2018.
// Copyright © 2018 GENIESOFT STUDIOS. All rights reserved.
//
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using TrackMyWalks.Models;
using TrackMyWalks.Services;

namespace TrackMyWalks.ViewModels
{
public class WalksMainPageViewModel : BaseViewModel
{
// Create our WalksListModel Observable Collection
public ObservableCollection<WalkDataModel> WalksListModel;

public WalksMainPageViewModel(INavigationService navService) :
base(navService)
{
}

// Instance method to add and retrieve our Walk Trail items
public async Task GetWalkTrailItems()
{
// Check our IsProcessBusy property to see if we are
// already processing
if (IsProcessBusy)
return;

// If we aren't processing, we need to set our IsProcessBusy
// property to true
IsProcessBusy = true;

// Populate our WalkListModel List Collection with items from our
// Microsoft Azure Web Service
WalksListModel = new ObservableCollection<WalkDataModel> await
AzureDatabase.GetWalkEntries());

// Set our IsProcessBusy property value back to false when finished
IsProcessBusy = false;
}

// Instance method to initialise the WalksMainPageViewModel
public override async Task Init()
{
await Task.Factory.StartNew(async () =>
{
// Call our GetWalkTrailItems method to populate our collection
await GetWalkTrailItems();
});
}
}
}

Let's take a look at what we covered in the preceding code snippet:

  1. We started by modifying our GetWalkTrailItems instance method to include the async keyword so that our method can handle asynchronous calls.
  2. Next, we created a WalksListModel ObservableCollection collection object that will raise an event whenever an object is added to or removed from our WalksListModel collection.
  3. Finally, we used the await keyword and called the GetWalkEntries instance method on our AzureDatabase property that we defined within our BaseViewModel to populate our WalksListModel collection.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.66.156