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
Daniel Hindrikes Daniel Hindrikes
Author Profile Icon Daniel Hindrikes
Daniel Hindrikes
Johan Karlsson Johan Karlsson
Author Profile Icon Johan Karlsson
Johan 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

Marking an item as complete using a command

We need to add a functionality that allows us to toggle items between complete and active. It is possible to navigate to the detailed view of the to-do list item, but this is too much work for a user. Instead, we'll add a ContextAction item to ListView. In iOS, for example, this is accessed by swiping left on a row:

  1. Open ViewModel/TodoItemViewModel.cs.
  2. Add a using statement for System.Windows.Input and Xamarin.Forms.
  3. Add a command to toggle the status of the item and a piece of text that describes the status:
public ICommand ToggleCompleted => new Command((arg) =>
{
Item.Completed = !Item.Completed;
ItemStatusChanged?.Invoke(this, new EventArgs());
});

Here, we have added a command for toggling the state of an item. When executed, it inverses the current state and raises the ItemStatusChanged event so that subscribers are notified. To change the text of the context action button depending on the status, we added a StatusText property. This is not recommended practice because we are adding code that only exists because of a specific UI case to ViewModel. Ideally, this would be handled by the view, perhaps by using ValueConverter. To save us from having to implement these steps, however, we have left it as a string property:

  1. Open Views/MainView.xaml.
  2. Locate the ListView.ItemTemplate node and add the following ViewCell.ContextActions node:
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Text="{Binding StatusText}"
Command="{Binding ToggleCompleted}" />
</ViewCell.ContextActions>
<Grid Padding="15,10">
...
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
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 £13.99/month. Cancel anytime
Visually different images