Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Xamarin.Forms Projects

You're reading from   Xamarin.Forms Projects Build multiplatform mobile apps and a game from scratch using C# and Visual Studio 2019

Arrow left icon
Product type Paperback
Published in Jun 2020
Publisher Packt
ISBN-13 9781839210051
Length 504 pages
Edition 2nd Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
 Hindrikes Hindrikes
Author Profile Icon Hindrikes
Hindrikes
 Karlsson Karlsson
Author Profile Icon Karlsson
Karlsson
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Introduction to Xamarin 2. Building Our First Xamarin.Forms App FREE CHAPTER 3. Building a News App Using Xamarin.Forms Shell 4. A Matchmaking App with a Rich UX Using Animations 5. Building a Photo Gallery App Using CollectionView and CarouselView 6. Building a Location Tracking App Using GPS and Maps 7. Building a Weather App for Multiple Form Factors 8. Setting Up a Backend for a Chat App Using Azure Services 9. Building a Real-Time Chat Application 10. Creating an Augmented Reality Game 11. Hot Dog or Not Hot Dog Using Machine Learning 12. Other Books You May Enjoy

Creating the MainViewModel

Up to this point, we have mainly prepared to write the code that will make up the app itself. MainViewModel is the ViewModel class for the first view that is displayed to the user. It is responsible for providing data and logic to a list of to-do list items. We will create the bare-bones ViewModel classes and add code to them as we progress through this chapter:

  1. Create a class called MainViewModel in the ViewModels folder.
  2. Add the following template code and resolve the references:
public class MainViewModel : ViewModel
{
private readonly TodoItemRepository repository;

public MainViewModel(TodoItemRepository repository)
{
this.repository = repository;
Task.Run(async () => await LoadData());
}

private async Task LoadData()
{

}
}

The structure of this class is something that we will reuse for all the ViewModel classes to come.

Let's summarize the important features we want the ViewModel class to have:

  • We inherit from the ViewModel class to gain access to shared logic, such as the INotifyPropertyChanged interface and common navigation code.
  • All dependencies to other classes, such as repositories and services, are passed through the constructor of ViewModel. This is handled by the dependency injection pattern and, more specifically for our case, by Autofac, which is the implementation of the dependency injection we are using.
  • We use an asynchronous call to LoadData() as an entry point to initialize the ViewModel class. Different MVVM libraries might do this in different ways, but the basic functionally is the same.
You have been reading a chapter from
Xamarin.Forms Projects - Second Edition
Published in: Jun 2020
Publisher: Packt
ISBN-13: 9781839210051
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at ₹800/month. Cancel anytime
Visually different images