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

Using ValueConverter

We want to use our brand new StatusColorConverter object in MainView. Unfortunately, we have to jump through some hoops to make this happen. We need to do three things:

  • Define a namespace in XAML
  • Define a local resource that represents an instance of the converter
  • Declare in the binding that we want to use the converter

Let's start with the namespace:

  1. Open Views/MainView.xaml.
  2. Add the following namespace to the page:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:converters="clr-namespace:DoToo.Converters"
x:Class="DoToo.Views.MainView"
Title="Do Too!>

Add a Resource node to the MainView.xaml file:

  1. Open Views/MainView.xaml.
  2. Add the following ResourceDictionary element, shown in bold under the root element of the XAML file:
<ContentPage ...>
<ContentPage.Resources>
<ResourceDictionary>
<converters:StatusColorConverter
x:Key="statusColorConverter" />

</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.ToolBarItems>
<ToolbarItem Text="Add" Command="{Binding AddItem}" />
</ContentPage.ToolbarItems>
<Grid ...>
</Grid>
</ContentPage>

This has the same form as the global resource dictionary, but since this one is defined in MainView, it can only be accessed from there. We could have defined this in the global resource dictionary, but it's usually more efficient to define objects that you only consume in one place as close to that place as possible.

The last step is to add the converter:

  1. Locate the BoxView node in the XAML file.
  2. Add the BackgroundColor XAML, which is marked in bold:
<BoxView Grid.RowSpan="2" 
BackgroundColor="{Binding Item.Completed,
Converter={StaticResource
statusColorConverter}}"
/>

What we have done here is bind a Boolean value to a property that takes a Color object. Right before the data binding takes place, however, ValueConverter converts the Boolean value into a color. This is just one of the many cases where ValueConverter comes in handy. Keep this in mind when you define the GUI.

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