Creating and implementing the NavigationService class

In this section, we will take a look at how to create the NavigationService class, which will inherit from our INavigationService interface and implement the underlying instance methods that we declared within our interface class to help navigate between our ViewModels.

Let's start by creating the NavigationService class for our TrackMyWalks app by performing the following steps:

  1. Ensure that the TrackMyWalks solution is open within the Visual Studio for Mac IDE.
  2. Next, right-click on the Services folder, and choose Add|New File... from the pop-up menu.
  3. Then, choose the Empty Class option under the General section and enter NavigationService for the name of the class to be created, as shown in the following screenshot:
Creating the NavigationService Class
  1. Next, click on the New button to allow the wizard to proceed and create the new file, as shown in the preceding screenshot. Now that we have created our NavigationService class, we can proceed with implementing the required code for our class.
  2. Locate and open the NavigationService.cs file, which is located as part of the TrackMyWalks group, and ensure that it is displayed within the code editor. Enter the following code snippet:
     //
// NavigationService.cs
// Navigation Service Class that each of our ViewModels will utilise
//
// Created by Steven F. Daniel on 16/06/2018.
// Copyright © 2018 GENIESOFT STUDIOS. All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using TrackMyWalks.Services;
using TrackMyWalks.ViewModels;
using Xamarin.Forms;

[assembly: Dependency(typeof(NavigationService))]
namespace TrackMyWalks.Services
{
public class NavigationService : INavigationService
{
public INavigation XFNavigation { get; set; }
readonly IDictionary<Type, Type> _viewMapping = new Dictionary<Type, Type>();

// Register our ViewModel and View within our Dictionary
public void RegisterViewMapping(Type viewModel, Type view)
{
_viewMapping.Add(viewModel, view);
}
// Removes the most recent Page from the navigation stack.
public Task<Page> RemoveViewFromStack()
{
return XFNavigation.PopAsync();
}
// Returns to the Root Page after removing the current page
// from the navigation stack
public Task BackToMainPage()
{
return XFNavigation.PopToRootAsync(true);
}
// Navigates navigates to a specific ViewModel
public async Task NavigateTo<TVM>() where TVM : BaseViewModel
{
await NavigateToView(typeof(TVM));
if (XFNavigation.NavigationStack.Last().BindingContext is BaseViewModel)
await ((BaseViewModel)(XFNavigation.NavigationStack.Last().
BindingContext)).Init();
}
// Navigates to a specific ViewModel within our dictionary
// viewMapping
public async Task NavigateToView(Type viewModelType)
{
Type viewType;
if (!_viewMapping.TryGetValue(viewModelType, out viewType))
throw new ArgumentException("No view found in View Mapping
for " + viewModelType.FullName + ".");

var constructor = viewType.GetTypeInfo().
DeclaredConstructors.FirstOrDefault(
dc =>!dc.GetParameters().Any());

var view = constructor.Invoke(null) as Page;
await XFNavigation.PushAsync(view, true);
}
}
}

Now, let's start by taking a look at what we covered in the preceding code snippet:

  1. First, we started by initializing our NavigationService class to be marked as a dependency by adding the Dependency metadata attribute so that it can be resolved by the Xamarin.Forms DependencyService class. This will enable our class to find and use the method implementations defined by our INavigationService interface.
  2. Next, we need to ensure that our NavigationService class inherits from the INavigationService interface so that it can access the instance methods, as well as any getters and setters.
  3. Then, we created an INavigationService property called XFNavigation, which will provide our class with a reference to the current navigation instance that will need to be set up when the navigation service class is first initialized within our App.xaml.cs file.
  4. Next, we declared a _viewMapping variable, which will be used to store all of the ViewModel and Views (Pages) mappings, prior to declaring a RegisterViewMapping instance method, which will be used to register each of our ViewModels and Views (Pages) within our Dictionary _viewMapping object.
  1. Next, we created our RemoveViewFromStack instance method, which will be responsible for removing the most recently added page from within our navigation stack, and created a BackToMainPage instance method, which will be responsible for removing all of the pages from the navigation stack, including the current page, and display the Root page.
  2. Then, we created our NavigateTo instance method, which will be used to navigate to a specific ViewModel that is contained within our _viewMapping Dictionary object, by calling the NavigateToView instance method, and then calling the Init method within the associated ViewModel to initialize the BindingContext.
  3. Finally, we created our NavigateToView instance method, which accepts the ViewModel as a parameter, and which will be used to navigate to the specified ViewModel within our _viewMapping object. We used the TryGetValue method to check to see whether the view can be found within the _viewMapping Dictionary. Then, we navigate to the ViewModel if it is found.
For more information on the DependencyService class, refer to the Microsoft Developer Documentation at https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction.
..................Content has been hidden....................

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