Table Of ContentUsing MVVM Light
with your
Xamarin Apps
—
Paul Johnson
Using MVVM Light with
your Xamarin Apps
Paul Johnson
Using MVVM Light with your Xamarin Apps
Paul Johnson
Merseyside, United Kingdom
ISBN-13 (pbk): 978-1-4842-2474-8 ISBN-13 (electronic): 978-1-4842-2475-5
https://doi.org/10.1007/978-1-4842-2475-5
Library of Congress Control Number: 2017962633
Copyright © 2018 by Paul Johnson
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage
and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or
hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are
not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material
contained herein.
Cover image designed by Freepik
Managing Director: Welmoed Spahr
Editorial Director: Todd Green
Acquisitions Editor: Todd Green
Development Editor: Laura Berendson
Technical Reviewer: Abhishek Sur
Coordinating Editor: Jill Balzano
Copy Editor: April Rondeau
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Distributed to the book trade worldwide by Springer Science+Business Media New York,
233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, email
Contents
About the Author ����������������������������������������������������������������������������������������������������� ix
About the Technical Reviewer ��������������������������������������������������������������������������������� xi
Acknowledgments ������������������������������������������������������������������������������������������������� xiii
Foreword �����������������������������������������������������������������������������������������������������������������xv
Introduction �����������������������������������������������������������������������������������������������������������xvii
■ Chapter 1: Installing MVVM Light �������������������������������������������������������������������������� 1
Creating Your Shell Project ����������������������������������������������������������������������������������������������� 1
Visual Studio 2015 & 2017 ��������������������������������������������������������������������������������������������������������������������� 1
VisualStudio for Mac ������������������������������������������������������������������������������������������������������������������������������� 3
Adding the MVVM Light Libraries ������������������������������������������������������������������������������������� 7
Adding MVVM Light to the Mac Project �������������������������������������������������������������������������������������������������� 7
Add MVVM Light to the Visual Studio Project ����������������������������������������������������������������������������������������� 9
Congratulations! ����������������������������������������������������������������������������������������������������������������������������������� 14
Conclusion ���������������������������������������������������������������������������������������������������������������������� 15
■ Chapter 2: Your First MVVM Light Mobile App� ���������������������������������������������������� 17
Welcome to Football Grounds ���������������������������������������������������������������������������������������� 17
How an MVVM Application Is Constructed �������������������������������������������������������������������������������������������� 17
Notifcations ����������������������������������������������������������������������������������������������������������������������������������������� 18
Let’s Design the App ������������������������������������������������������������������������������������������������������� 19
Constructing the Model ������������������������������������������������������������������������������������������������������������������������ 19
Constructing the View Model ���������������������������������������������������������������������������������������������������������������� 20
Setting the ViewModelLocator �������������������������������������������������������������������������������������������������������������� 20
iii
■ Contents
Constructing the User Interface (View) ������������������������������������������������������������������������������������������������� 21
The View Model ������������������������������������������������������������������������������������������������������������������������������������ 22
The Platform User Interface ������������������������������������������������������������������������������������������� 24
1� Android ��������������������������������������������������������������������������������������������������������������������������������������������� 24
2� iOS ���������������������������������������������������������������������������������������������������������������������������������������������������� 28
3� Windows Mobile ������������������������������������������������������������������������������������������������������������������������������� 30
Accessing the ViewModelLocator ��������������������������������������������������������������������������������������������������������� 30
Compiling and Running the Apps ����������������������������������������������������������������������������������� 32
Extending Our Application ���������������������������������������������������������������������������������������������� 37
Adding the Navigation Service ������������������������������������������������������������������������������������������������������������� 38
Using the Navigation Service Within the View Model ��������������������������������������������������������������������������� 40
Adding Access to Maps: iOS ����������������������������������������������������������������������������������������������������������������� 41
Adding Access to Maps: Windows Mobile �������������������������������������������������������������������������������������������� 41
Adding Access to Maps: Android ���������������������������������������������������������������������������������������������������������� 41
Putting Maps into the Apps: iOS ����������������������������������������������������������������������������������������������������������� 42
Putting Maps into the Apps: Android ����������������������������������������������������������������������������������������������������� 43
Putting Maps into the Apps: Windows Mobile ��������������������������������������������������������������������������������������� 44
Creating the Location from the View Model� ����������������������������������������������������������������������������������������� 45
Plotting the Data ����������������������������������������������������������������������������������������������������������������������������������� 46
Conclusion ���������������������������������������������������������������������������������������������������������������������� 47
■ Chapter 3: Inversion of Control (IoC) & Messaging ��������������������������������������������� 49
IoC Basics ����������������������������������������������������������������������������������������������������������������������� 50
IoC��������������������������������������������������������������������������������������������������������������������������������������������������������� 50
Using the Built-in IoC� ����������������������������������������������������������������������������������������������������� 56
Dependency Injection ��������������������������������������������������������������������������������������������������������������������������� 57
Messaging �������������������������������������������������������������������������������������������������������������������������������������������� 60
Register ������������������������������������������������������������������������������������������������������������������������������������������������ 60
Unregister ��������������������������������������������������������������������������������������������������������������������������������������������� 61
Send ����������������������������������������������������������������������������������������������������������������������������������������������������� 61
iv
■ Contents
Other Methods in the Messenger Class ������������������������������������������������������������������������� 61
Default �������������������������������������������������������������������������������������������������������������������������������������������������� 61
Reset����������������������������������������������������������������������������������������������������������������������������������������������������� 61
Cleanup ������������������������������������������������������������������������������������������������������������������������������������������������� 62
Other Important Classes ����������������������������������������������������������������������������������������������������������������������� 62
Using the Messenger Class in action ���������������������������������������������������������������������������������������������������� 62
Conclusion ���������������������������������������������������������������������������������������������������������������������� 65
■ Chapter 4: Adding Functionality �������������������������������������������������������������������������� 67
Adding a Database ��������������������������������������������������������������������������������������������������������� 67
Creating Your SQLite Application ���������������������������������������������������������������������������������������������������������� 71
Models, Interfaces, and Helpers ����������������������������������������������������������������������������������������������������������� 73
Wiring the Database Up for the View Models ��������������������������������������������������������������������������������������� 75
Let’s Extend This a Bit: Using the Device Address Book ����������������������������������������������������������������������� 77
How Do We Know When All of the Data Has Been Read? ��������������������������������������������������������������������� 81
Passing the Data ID Between the View Models ������������������������������������������������������������������������������������ 82
Adding the Component to the Windows Phone Project ������������������������������������������������������������������������ 83
Displaying Data ������������������������������������������������������������������������������������������������������������������������������������� 86
Connecting to a Webservice ������������������������������������������������������������������������������������������� 87
Checking for Connectivity ��������������������������������������������������������������������������������������������������������������������� 89
GPS ������������������������������������������������������������������������������������������������������������������������������������������������������� 90
GPS with the Plugin ������������������������������������������������������������������������������������������������������������������������������ 90
Conclusion ���������������������������������������������������������������������������������������������������������������������� 92
■ Chapter 5: Converting Your Existing Apps ����������������������������������������������������������� 93
The Original App ������������������������������������������������������������������������������������������������������������� 93
The Redesign Process ���������������������������������������������������������������������������������������������������� 95
The User Interface Elements ���������������������������������������������������������������������������������������������������������������� 96
Thinking in MVVM ��������������������������������������������������������������������������������������������������������������������������������� 97
Let’s Get the Show on the Road ������������������������������������������������������������������������������������� 98
Code Analysis ��������������������������������������������������������������������������������������������������������������������������������������� 98
Dealing with Events ���������������������������������������������������������������������������������������������������������������������������� 100
v
■ Contents
Moving the Code Over to the PCL ������������������������������������������������������������������������������������������������������� 100
The View Models ��������������������������������������������������������������������������������������������������������������������������������� 103
Data Persistence��������������������������������������������������������������������������������������������������������������������������������� 108
Platform User Interfaces ��������������������������������������������������������������������������������������������������������������������� 109
Events on Windows Phone ������������������������������������������������������������������������������������������������������������������ 115
Putting Everything Together���������������������������������������������������������������������������������������������������������������� 115
Conclusion �������������������������������������������������������������������������������������������������������������������� 116
■ Chapter 6: The Outside World ���������������������������������������������������������������������������� 117
What Is Involved? ��������������������������������������������������������������������������������������������������������� 117
Introducing the Meeting Planner �������������������������������������������������������������������������������������������������������� 118
Async Versus Sync ������������������������������������������������������������������������������������������������������������������������������ 121
Let Me Introduce You to the Key Difference Between iOS and Everything Else ��������������������������������� 122
The Connectivity Service �������������������������������������������������������������������������������������������������������������������� 124
Defensive Programming ��������������������������������������������������������������������������������������������������������������������� 125
Let’s Have a Look at the App in Action ����������������������������������������������������������������������������������������������� 126
Do We Need to Access the Outside World? ����������������������������������������������������������������������������������������� 129
How Can We Notify the UI? ����������������������������������������������������������������������������������������������������������������� 131
Conclusion �������������������������������������������������������������������������������������������������������������������� 132
■ Chapter 7: Unit Testing �������������������������������������������������������������������������������������� 133
A Quick Introduction ����������������������������������������������������������������������������������������������������� 133
Let’s Make a Start and Add a Unit-Testing Project ����������������������������������������������������������������������������� 133
The Anatomy of a Unit Test ����������������������������������������������������������������������������������������������������������������� 136
Let’s Get Real �������������������������������������������������������������������������������������������������������������������������������������� 140
Database Testing �������������������������������������������������������������������������������������������������������������������������������� 140
Testing Online Services ���������������������������������������������������������������������������������������������������������������������� 144
Unit Testing the User Interface ������������������������������������������������������������������������������������� 148
Setting Up Your Project ����������������������������������������������������������������������������������������������������������������������� 149
Unit Testing a Xamarin Forms App ������������������������������������������������������������������������������� 155
Conclusion �������������������������������������������������������������������������������������������������������������������� 155
vi
■ Contents
■ Chapter 8: Using Xamarin Forms ����������������������������������������������������������������������� 157
Setting Up Your project ������������������������������������������������������������������������������������������������� 157
Creating Your Forms App with Visual Studio ��������������������������������������������������������������������������������������� 157
Creating a Forms App on Visual Studio ����������������������������������������������������������������������������������������������� 161
Adding the MVVM Project to the Forms App on VisualStudio for Mac ������������������������������������������������ 164
Adding the MVVM Project to Visual Studio ����������������������������������������������������������������������������������������� 167
Adding the MVVM Framework ������������������������������������������������������������������������������������������������������������ 168
Where Setting Up for Forms Differs ���������������������������������������������������������������������������������������������������� 169
Data Binding ��������������������������������������������������������������������������������������������������������������������������������������� 171
Accessing the View Model from Within the ContentPage ������������������������������������������������������������������� 174
Binding Within XAML �������������������������������������������������������������������������������������������������������������������������� 175
Code Localization ������������������������������������������������������������������������������������������������������������������������������� 175
Can You See a Problem with This? ����������������������������������������������������������������������������������������������������� 177
File Handling ��������������������������������������������������������������������������������������������������������������������������������������� 179
Settings ���������������������������������������������������������������������������������������������������������������������������������������������� 180
Creating a Responsive Application ����������������������������������������������������������������������������������������������������� 181
Can We Do Anything Else to Increase the Performance? �������������������������������������������������������������������� 181
Conclusion �������������������������������������������������������������������������������������������������������������������� 181
■ Chapter 9: Rounding Things Off ������������������������������������������������������������������������� 183
PCL Versus SCL—What’s the Difference? �������������������������������������������������������������������� 183
Removing the Need for #if Statements ����������������������������������������������������������������������������������������������� 184
SCL and NuGet Packages ������������������������������������������������������������������������������������������������������������������� 184
SCL and MVVM Light �������������������������������������������������������������������������������������������������������������������������� 185
Changing Your Code to Run with the SCL ������������������������������������������������������������������������������������������� 186
Converting an Existing Package to Use the SCL Version �������������������������������������������������������������������� 187
Creating the SCL Without Converting a PCL���������������������������������������������������������������������������������������� 187
vii
■ Contents
A Practical Example: Playing Audio ������������������������������������������������������������������������������ 187
Application Lifecycle Handling ������������������������������������������������������������������������������������� 189
iOS ������������������������������������������������������������������������������������������������������������������������������������������������������ 191
How Can We Use the MVVM Model for the Lifecycle? ����������������������������������������������������������������������� 192
Threading: Let Buyer Beware ��������������������������������������������������������������������������������������� 193
How to Help with This Race Condition ������������������������������������������������������������������������������������������������ 194
Conclusion �������������������������������������������������������������������������������������������������������������������� 195
Index ��������������������������������������������������������������������������������������������������������������������� 197
viii
About the Author
Paul Johnson is a 46-year-old mobile software developer. He has written code for many companies
(including TfL, NHS, and Farm Apps) and most of the time has enjoyed it. He lives with his wife and
daughter along with a variety of animals.
He loves to travel (Australia being a particular favorite destination) and has a long-term love of retro
computing—in particular the old 8-bit home micros of the 1980s.
This is his fourth book (first with Apress), and he has plans for a fifth already.
Outside of developing software, he is an avid scuba diver and can frequently be found diving at local
quarries. He loves reading books as well as listening to rock and metal.
According to another bio, he doesn’t have a pet badger. He doesn’t. He does have a lot of coffee stains
on his desk.
ix