Download Mobile Development with NET Build cross platform mobile applications with Xamarin Forms 5 and ASP NET Core 5 2nd Edition Can Bilgin ebook All Chapters PDF
Download Mobile Development with NET Build cross platform mobile applications with Xamarin Forms 5 and ASP NET Core 5 2nd Edition Can Bilgin ebook All Chapters PDF
com
https://textbookfull.com/product/mobile-development-with-
net-build-cross-platform-mobile-applications-with-xamarin-
forms-5-and-asp-net-core-5-2nd-edition-can-bilgin/
OR CLICK BUTTON
DOWNLOAD NOW
https://textbookfull.com/product/beginning-visual-studio-for-mac-
build-cross-platform-apps-with-xamarin-and-net-core-alessandro-del-
sole/
textboxfull.com
https://textbookfull.com/product/cross-platform-localization-for-
native-mobile-apps-with-xamarin-1st-edition-christopher-miller/
textboxfull.com
Building Microservices with ASP NET Core Develop Test and
Deploy Cross Platform Services in the Cloud 1st Edition
Kevin Hoffman
https://textbookfull.com/product/building-microservices-with-asp-net-
core-develop-test-and-deploy-cross-platform-services-in-the-cloud-1st-
edition-kevin-hoffman/
textboxfull.com
https://textbookfull.com/product/c-7-and-net-core-modern-cross-
platform-development-2nd-edition-mark-j-price/
textboxfull.com
https://textbookfull.com/product/mobile-app-development-with-ionic-
cross-platform-apps-with-ionic-angular-and-cordova-griffith/
textboxfull.com
Can Bilgin
BIRMINGHAM—MUMBAI
Mobile Development with .NET
Second Edition
Copyright © 2021 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or
transmitted in any form or by any means, without the prior written permission of the publisher,
except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the
information presented. However, the information contained in this book is sold without warranty,
either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors,
will be held liable for any damages caused or alleged to have been caused directly or indirectly by
this book.
Packt Publishing has endeavored to provide trademark information about all of the companies
and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing
cannot guarantee the accuracy of this information.
ISBN 978-1-80020-469-0
www.packt.com
To my beloved wife and best friend for life, Sanja.
– Can Bilgin
Contributors
About the author
Can Bilgin is a solution architect, working for Authority Partners Inc. He has been
working in the software industry for almost two decades on various consumer- and
enterprise-level engagements for high-profile clients using technologies such as BizTak,
Service Fabric, Orleans, Dynamics CRM, Xamarin, WCF, Azure services, and other web/
cloud technologies. His passion lies in mobile and IoT development using modern tools
available to developers. He shares his experience on his blog, on social media, and through
speaking engagements at local and international community events. He was recognized as
a Microsoft MVP for his technical contributions between 2014 and 2018.
I want to thank my wife and daughter for their tremendous support during
this project.
About the reviewer
Ahmed Ilyas has 18 years of professional experience in software development.
After leaving Microsoft, he ventured into setting up his own consultancy company,
offering the best possible solutions for a number of industries and providing real-
world answers to those problems. He uses the Microsoft stack to not only build these
technologies to be able to provide the best practices, patterns, and software to his client
base to enable long-term stability and compliance in the ever-changing software industry
but also to improve the skills of software developers around the globe as well as pushing
the limits of technology.
This has led to him being awarded MVP in C# by Microsoft three times for "providing
excellence and independent real-world solutions to problems that developers face."
His great reputation has resulted in him having a large client base for his consultancy
company, Sandler Ltd (UK) and Sandler Software LLC (US), which includes clients
from different industries. Clients have included him on their "approved contractors/
consultants" list as a trusted vendor and partner.
Ahmed Ilyas has also been involved in the past in reviewing books for Packt Publishing
and wishes to thank them for the great opportunity once again.
I would like to thank the author/publisher of this book for giving me the
great honor and privilege of reviewing the book, as well as, especially,
Microsoft Corporation and my colleagues for enabling me to become a
reputable leader as a software and solutions developer in the industry.
Table of Contents
Preface
Section 1:
Understanding .NET
1
Getting Started with .NET 5.0
Technical Requirements 4 Developing with .NET 5.0 9
Exploring cross-platform Creating a runtime-agnostic application 9
development4 Defining a runtime and self-contained
Developing fully native applications 5 deployment14
Hybrid applications 5 Defining a framework 17
Native cross-platform frameworks 6
Summary19
Understanding .NET Core 7
2
Defining Xamarin, Mono, and .NET Standard
Understanding Xamarin 22 Using .NET with Xamarin 35
Setting up Your Development Xamarin.Forms 37
Environment22 Extending the reach 42
Creating your First Xamarin Summary 43
Application25
Xamarin on Android – Mono Droid 26
Xamarin on iOS – Mono Touch 32
ii Table of Contents
3
Developing with Universal Windows Platform
Introducing Universal Windows Native with UWP 54
Platform46 Working with Platform
Creating UWP applications 47 extensions55
Understanding XAML Differences51 Summary57
Using .NET Standard and .NET
Section 2:
Xamarin and Xamarin.Forms
4
Developing Mobile Applications with Xamarin
Technical Requirements 62 implementation72
Choosing between Xamarin Architectural Patterns for
and Xamarin.Forms 62 Unidirectional Data Flow 81
5
UI Development with Xamarin
Technical Requirements 88 Implementing navigation
Application layout 88 structure94
Consumer expectations 88 Single-page view 94
Platform imperatives 91 Simple navigation 100
Development cost 93 Multi-page views 103
Table of Contents iii
6
Customizing Xamarin.Forms
Technical Requirements 142 Platform specifics 159
Xamarin.Forms development Xamarin.Forms effects 160
domains142 Composite customizations 164
Section 3:
Azure Cloud Services
7
Azure Services for Mobile Applications
An overview of Azure services 186 Azure storage 202
An introduction to distributed systems 186 Cosmos DB 204
Cloud architecture 190 Azure Cache for Redis 205
Azure service providers and resource
Azure Serverless 205
types198
Azure functions 205
Data stores 201 Azure Logic apps 208
Relational database resources 201 Azure Event Grid 209
iv Table of Contents
8
Creating a Datastore with Cosmos DB
Technical Requirements 214 Creating and accessing documents 225
The basics of Cosmos DB 214 Denormalized data 231
Referenced data 234
Global distribution 216
Consistency spectrum 216 Cosmos DB in depth 236
Pricing219
Partitioning 236
Data access models 220 Indexing 239
Programmability241
The SQL API 220
The change feed 245
The MongoDB API 220
Others224 Summary245
Modeling data 224
9
Creating Microservices Azure App Services
Technical requirements 248 Implementing a soft delete 267
Choosing the right app model 248 Integrating with Redis cache 268
Azure virtual machines 249
Hosting the services 271
Containers in Azure 250
Azure Web App for App Service 271
Microservices with Azure
Containerizing services 274
Service Fabric 251
Azure App Service 252 Securing the application 277
Creating our first microservice 253 ASP.NET Core Identity 279
Azure AD 280
Initial setup 253
Azure AD B2C 285
Implementing retrieval actions 256
Implementing update methods 264 Summary286
Table of Contents v
10
Using .NET Core for Azure Serverless
Understanding Azure Using connectors 304
Serverless288 Creating our first Logic App 305
Developing Azure Functions 288 Workflow execution control 310
Using Azure Function Runtimes 289 Integration with Azure services 313
Function triggers and bindings 294
Repository 313
Configuring functions 295
Queue-based processing 314
Hosting functions 296
Event aggregation 315
Creating our first Azure function 297
Summary 316
Developing a Logic App 300
Implementing Logic Apps 301
Section 4:
Advanced Mobile Development
11
Fluid Applications with Asynchronous Patterns
Utilizing tasks and awaitables 320 Asynchronous event handling 344
Task-based execution 321 The asynchronous command 346
Synchronization context 327
Native asynchronous
Single execution guarantee 329
execution 349
Logical tasks 331
Android services 349
The command pattern 332
iOS backgrounding 350
Creating producers/consumers 335
Using observables and data streams 337 Summary 352
Asynchronous execution
patterns 342
Service initialization pattern 342
vi Table of Contents
12
Managing Application Data
Improving HTTP performance SQLite.NET 361
with transient caching 354 Entity Framework Core 363
Client cache aside 355
Data access patterns 364
Entity tag (ETag) validation 357
Implementing the repository pattern 365
Key/value store 359
Observable repository 367
Persistent relational
data cache 361 Summary 373
13
Engaging Users with Notifications and the Graph API
Understanding Native Device registration 384
Notification Services 376 Transmitting notifications 388
Notification providers 376 Broadcasting to multiple devices 390
Sending notifications with PNS 377 Advanced scenarios 391
General constraints 378
The Graph API and
Azure Notification Hubs 379 Project Rome 393
Notification Hubs infrastructure 379 The Graph API 393
Notifications using Azure Notification Project Rome 393
Hubs 381
Summary 396
Creating a notification service 383
Defining the requirements 383
Section 5:
Application Life Cycle Management
14
Azure DevOps and Visual Studio App Center
Using Azure DevOps and Git 400 Branching strategy 402
Creating a Git repository with Managing development branches 404
Azure DevOps 400
Table of Contents vii
15
Application Telemetry with Application Insights
Collecting insights for Xamarin Application Insights data model 443
applications 430 Collecting telemetry data with
The telemetry data model 430 ASP.NET Core 444
Advanced application telemetry 435 Collecting telemetry with Azure
Functions449
Exporting App Center telemetry
data to Azure 440
Analyzing data 451
Collecting telemetry data for Summary454
Azure Service 443
16
Automated Testing
Maintaining application Implementing platform tests 473
integrity with tests 456
Automated UI tests 473
Arrange, Act, and Assert 456
Xamarin.UITests474
Creating unit tests with mocks 462
Page Object Pattern 477
Fixtures and data-driven tests 466
Summary481
Maintaining cross-module
integrity with integration tests 469
Testing client-server communication 469
viii Table of Contents
17
Deploying Azure Modules
Creating an ARM template 484 Deploying .NET
ARM template concepts 491 Core applications 499
Using Azure DevOps for ARM Summary 503
templates495
18
CI/CD with Azure DevOps
Introducing CI/CD 506 Testing519
CI/CD with GitFlow 508 Static code analysis with SonarQube 520
Chapter 5, UI Development with Xamarin, takes a look at certain UI patterns that allow
developers and user experience designers to come to a compromise between the user
expectations and product demands in order to create a platform and product with a
consistent user experience across platforms.
Chapter 6, Customizing Xamarin.Forms, goes through the steps and procedures of
customizing Xamarin.Forms without compromising on the performance or user
experience. Some of the features that will be analyzed include effects, behaviors,
extensions, and custom renderers.
Chapter 7, Azure Services for Mobile Applications, discusses the fact that there are a
number of services that are offered as services (SaaS), platform (PaaS), or infrastructure
(IaaS), such as Notification Hubs, Cognitive Services, and Azure Functions, that can
change the impressions of users regarding your application with little or no additional
development hours. This chapter will give you a quick overview of using some of these
services when developing .NET Core applications.
Chapter 8, Creating a Datastore with Cosmos DB, explains how Cosmos DB offers a multi-
model and multi-API paradigm that allows applications to use multiple data models
while storing application data with the most suitable API for the application, such as SQL,
JavaScript, Gremlin, and MongoDB. In this chapter, we will create the datastore for our
application and implement the data access modules.
Chapter 9, Creating Microservices Azure App Services, goes through the basics of Azure
App Service, and we will create a simple, data-oriented backend for our application using
ASP.NET Core with authentication provided by Azure Active Directory. Additional
implementation will include offline sync and push notifications.
Chapter 10, Using .NET Core for Azure Serverless, shows how to incorporate Azure
Functions into our infrastructure to process data on different triggers, and how to
integrate Azure Functions with a logic app that will be used as a processing unit in our
setup.
Chapter 11, Fluid Applications with Asynchronous Patterns, explains that when developing
Xamarin applications and ASP.NET Core applications, both the task's framework and
the reactive modules can help distribute the execution threads and create a smooth and
uninterrupted execution flow. This chapter will go over some of the patterns associated
with these modules and apply them to various sections of the application.
Preface xi
Chapter 12, Managing Application Data, explains that in order to avoid data conflicts
and synchronization issues, developers must be diligent regarding the procedures
implemented according to the type of data at hand. This chapter will discuss the possible
data synchronization and offline storage scenarios using products such as SQLite and
Entity Framework Core, as well as the out-of-the-box offline support provided by Azure
App Service.
Chapter 13, Engaging Users with Notifications and the Graph API, briefly explains how
notifications and the graph API can be used to improve user engagement by taking
advantage of push notifications and the graph API. We will create a notification
implementation for cross-platform applications using Azure Notification Hubs. We will
also create so-called activity entries for our application sessions so that we can create a
timeline that is accessible on multiple platforms.
Chapter 14, Azure DevOps and Visual Studio App Center, shows how to use Visual Studio
Team Services and App Center to set up a complete, automated pipeline for Xamarin
applications that will connect the source repository to the final store submission.
Chapter 15, Application Telemetry with Application Insights, explains how Application
Insights is a great candidate for collecting telemetry from Xamarin applications that use
an Azure-hosted web service infrastructure because of its intrinsic integration with Azure
modules, as well as the continuous export functionality for App Center telemetry.
Chapter 16, Automated Testing, discusses how to create unit and coded UI tests, and
the architectural patterns that revolve around them. Data-driven unit tests, mocks, and
Xamarin UI tests are some of the concepts that will be discussed.
Chapter 17, Deploying Azure Modules, demonstrates how to configure the ARM template
for the Azure web service implementation, as well as other services (such as Cosmos DB
and Notification Hubs) that we used previously so that we can create deployments using
the Visual Studio Team Services build and release pipeline. Introducing configuration
values into the template and preparing it to create staging environments are our primary
focuses in this chapter.
Chapter 18, CI/CD with Azure DevOps, explains how developers can create fully
automated templates for builds, testing, and deployments using the toolset provided with
Visual Studio Team Services. In this chapter, we will set up the build and release pipeline
for Xamarin in line with the Azure deployment pipeline.
xii Preface
The IDE of choice for implementing the code walk-throughs is Visual Studio 2019 on
Windows and Visual Studio for Mac on macOS. Visual Studio Code, which supports both
platforms, can be used to create the scripting and Python examples.
If you are using the digital version of this book, we advise you to type the code yourself
or access the code via the GitHub repository (link available in the next section). Doing
so will help you avoid any potential errors related to the copying and pasting of code.
Conventions used
There are a number of text conventions used throughout this book.
Code in text: Indicates code words in text, database table names, folder names,
filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles.
Here is an example: "The IObserver and IObservable interfaces, which form the
basis for observables and so-called reactive patterns."
A block of code is set as follows:
namespace FirstXamarinFormsApplication
{
public partial class MainPage : ContentPage
{
InitializeComponent();
BindingContext = new MainPageViewModel();
}
}
When we wish to draw your attention to a particular part of a code block, the relevant
lines or items are set in bold:
public App()
{
InitializeComponent();
MainPage = NavigationPage(new ListItemView())
}
Bold: Indicates a new term, an important word, or words that you see onscreen. For
example, words in menus or dialog boxes appear in the text like this. Here is an example:
"Both the ALM process and the version control options are available under the Advanced
section of the project settings."
Get in touch
Feedback from our readers is always welcome.
General feedback: If you have questions about any aspect of this book, mention the book
title in the subject of your message and email us at [email protected].
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes
do happen. If you have found a mistake in this book, we would be grateful if you would
report this to us. Please visit www.packtpub.com/support/errata, selecting your
book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the Internet,
we would be grateful if you would provide us with the location address or website name.
Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in
and you are interested in either writing or contributing to a book, please visit authors.
packtpub.com.
Reviews
Please leave a review. Once you have read and used this book, why not leave a review on
the site that you purchased it from? Potential readers can then see and use your unbiased
opinion to make purchase decisions, we at Packt can understand what you think about
our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packt.com.
Section 1:
Understanding .NET
In this chapter, we will analyze cross-platform development tools and frameworks for
mobile applications and take an initial look at .NET Core development.
The following sections will guide you through getting started with .NET 5.0:
Technical Requirements
You can find the code used in this chapter through the following GitHub link:
https://github.com/PacktPublishing/Mobile-Development-with-.
NET-Second-Edition/tree/master/chapter01/src.
In order to meet these demands, we can choose to implement the application in several
different ways:
Hybrid applications
Native hosted web applications (also known as hybrid applications) are another
popular choice for (especially mobile) developers. In this architecture, a responsive web
application would be hosted on a thin native harness on the target platform. The native
web container would also be responsible for providing access to the web runtime on
native platform APIs. These hybrid applications wouldn't even need to be packaged as
application packages, but as Progressive Web Apps (PWAs) so that users can access
them directly from their web browsers. While the development resources are used more
efficiently than in the native cross-platform framework approach, this type of application
is generally prone to performance issues.
In reference to the business requirements at hand, we would probably develop a web
service layer and a small Single - Page Application (SPA), part of which is packaged
as a hybrid application.
6 Getting Started with .NET 5.0
Finally, in 2020, .NET Core replaced classic .NET. The unified .NET platform now
provides developers with a single runtime and framework that can be used to create
cross-platform applications using a single code base.
Semantically speaking, .NET now describes the complete infrastructure for the whole set
of cross-development tools that rely on a common language infrastructure and multiple
runtimes, including .NET Core Runtime, .NET, also known as Big CLR, the Mono
runtime, and Xamarin:
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside
the United States, check the laws of your country in addition to
the terms of this agreement before downloading, copying,
displaying, performing, distributing or creating derivative works
based on this work or any other Project Gutenberg™ work. The
Foundation makes no representations concerning the copyright
status of any work in any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if
you provide access to or distribute copies of a Project
Gutenberg™ work in a format other than “Plain Vanilla ASCII” or
other format used in the official version posted on the official
Project Gutenberg™ website (www.gutenberg.org), you must,
at no additional cost, fee or expense to the user, provide a copy,
a means of exporting a copy, or a means of obtaining a copy
upon request, of the work in its original “Plain Vanilla ASCII” or
other form. Any alternate format must include the full Project
Gutenberg™ License as specified in paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com