Mastering Xamarin.Forms
eBook - ePub

Mastering Xamarin.Forms

  1. 192 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Mastering Xamarin.Forms

Book details
Book preview
Table of contents
Citations

About This Book

Create high-quality multi-platform native apps with Xamarin.Forms

Key Features

  • Packed with real-world scenarios and solutions to help you build professional-grade mobile apps with Xamarin.Forms
  • Build an effective mobile app architecture with the Xamarin.Forms toolkit
  • Find out how, when, and why you should use architectural patterns and get best practices with Xamarin.Forms

Book Description

Discover how to extend and build upon the components of the Xamarin.Forms toolkit to develop an effective, robust mobile app architecture. Starting with an app built with the basics of the Xamarin.Forms toolkit, you'll go step by step through several advanced topics to create a solution architecture rich with the benefits of good design patterns and best practices.

You'll start by introducing a core separation between the app's user interface and its business logic by applying the MVVM pattern and data-binding. Then you focus on building out a layer of plugin-like services that handle platform-specific utilities such as navigation and geo-location, and on how to loosely use these services in the app with inversion of control and dependency injection. Next you connect the app to a live web-based API and set up offline synchronization. Then, you delve into testing the app logic through unit tests. Finally, you set up Visual Studio App Center for monitoring usage and bugs to gain a proactive edge on app quality.

What you will learn

  • Implement the Model-View-View-Model (MVVM) pattern and data-binding in Xamarin.Forms mobile apps
  • Extend the Xamarin.Forms navigation API with a custom ViewModel-centric navigation service
  • Leverage the inversion of control and dependency injection patterns in Xamarin.Forms mobile apps
  • Work with online and offline data in Xamarin.Forms mobile apps
  • Test business logic in Xamarin.Forms mobile apps
  • Use platform-specific APIs to build rich custom user interfaces in Xamarin.Forms mobile apps
  • Explore how to improve mobile app quality using Visual Studio AppCenter

Who this book is for

This book is intended for C# developers who are familiar with the Xamarin platform and the Xamarin.Forms toolkit. If you have already started working with Xamarin.Forms and want to take your app to the next level with higher quality, maintainability, testability, and flexibility, then this book is for you.

Frequently asked questions

Simply head over to the account section in settings and click on ā€œCancel Subscriptionā€ - itā€™s as simple as that. After you cancel, your membership will stay active for the remainder of the time youā€™ve paid for. Learn more here.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Both plans give you full access to the library and all of Perlegoā€™s features. The only differences are the price and subscription period: With the annual plan youā€™ll save around 30% compared to 12 months on the monthly plan.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, weā€™ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes, you can access Mastering Xamarin.Forms by Ed Snider in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in C#. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781788297349
Edition
2

Navigation

The overarching goal of this book is to show how you can build a solid architecture based on design patterns and best practices; the objective of this chapter is to take our TripLog app one step closer to achieving that goal. By introducing MVVM into our TripLog app in Chapter 2, MVVM and Data Binding, we set up the app with a very clear pattern to separate the user interface from the rest of the logic in the app. Each subsequent chapter, starting with this one, further advances this concept of separation.
In Chapter 2, MVVM and Data Binding, we moved a large portion of the app logic into ViewModels; however, navigation is still being initiated from the Pages (Views). In this chapter, we will also move navigation into ViewModels.
Here is a quick look at what we'll cover in this chapter:
  • Understanding the basics of the Xamarin.Forms navigation API
  • Thinking about navigation in MVVM
  • Creating a navigation service
  • Updating the TripLog app to use the navigation service

The Xamarin.Forms navigation API

Along with abstracting common user interface elements into a multi-platform API, Xamarin.Forms also abstracts navigation for iOS, Android, and Windows into a single easy-to-use navigation service. Each mobile platform does navigation in a slightly different way and has a slightly different navigation API; however, at their core, they all accomplish similar tasks and in most cases use a stack structureā€”last in, first out.
The Xamarin.Forms navigation API uses stack-like terminology, closely resembling the navigation APIs of iOS. The Xamarin.Forms navigation API is exposed through the Xamarin.Forms.INavigation interface, which is implemented via the Navigation property that can be called from any Xamarin.Forms.VisualElement object, but typically Xamarin.Forms.Page. Xamarin.Forms.NavigationPage also implements the INavigation interface and exposes public methods to perform common navigation tasks.
The Xamarin.Forms navigation API supports two types of navigation: standard and modal. Standard navigation is the typical navigation pattern where the user clicks or taps through a series of pages and is able to use either device/operating system-provided functionality (back buttons on Android and Windows), or app-provided elements (navigation bar on iOS and action bar on Android), to navigate back through the stack. Modal navigation is similar to the modal dialog concept in web apps where a new page is layered on top of the calling page, preventing interaction with the calling page until the user performs a specific action to close the modal page. On smaller form factor devices, modal pages typically take up the entire screen, whereas on larger form factors such as tablets, modal pages may only take up a subset of the screen, more like a dialog. The Xamarin.Forms.INavigation interface exposes two separate read-only properties to view the standard and modal navigation stacks: NavigationStack and ModalStack.
The Xamarin.Forms.INavigation interface provides several methods to asynchronously push and pop pages onto the navigation and modal stacks, as follows:
  • PushAsync(Page page) and PushAsync(Page page, bool animated) to navigate to a new page
  • PopAsync() and PopAsync(bool animated) to navigate back to the previous page, if there is one
  • PushModalAsync(Page page) and PushModalAsync(Page page, bool animated) to modally display a page
  • PopModalAsync() and PopModalAsync(bool animated) to dismiss the current modally displayed page
In addition to these methods, there are also a few methods that help you manipulate the navigation stack since it is exposed as a read-only property:
  • InsertPageBefore(Page page, Page before) to insert a page before a specific page that is already in the navigation stack
  • RemovePage(Page page) to remove a specific page in the navigation stack
  • PopToRootAsync() and PopToRootAsync(bool animated) to navigate back to the first page and remove all others in the navigation stack
We've already used PushAsync a few times in the TripLog app to allow the user to move from page to page. In the next couple of sections of this chapter, we'll create a custom navigation service that extends the Xamarin.Forms navigation API, use it to move those instances of PushAsync from the Views into the ViewModels, and expose them through commands that will be data bound to the Page.

Navigation and MVVM

One of the key purposes of the MVVM pattern is to isolate an app's presentation layer from its other layers. In doing so, an app's business logic is also isolated. One of the thoughts behind this isolation is to have a user interface that is only concerned with displaying data, and that is completely independent of how that data is stored, acquired, manipulated, or shared with the rest of the app. As explained in Chapter 2, MVVM and Data Binding, this is typically accomplished through data binding. In MVVM, the actions that a user performs on a page are bound to commands on that page's backing ViewModel. It is very common for these actions to result in a transition to another pageā€”either by directly linking to it or by automatically navigating to a previous page after performing a task, such as saving data. Therefore, it makes sense to rethink how we implement navigation in an app that leverages the MVVM pattern so that it can be controlled by the ViewModels and not by the pages.
Most of the common third-party MVVM frameworks and toolkits subscribe to this theory and often even provide a navigation service that is designed for ViewModel consumption.
There are two main approaches to consider when performing n...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Dedication
  4. www.packtpub.com
  5. Foreword
  6. Contributors
  7. Preface
  8. Getting Started
  9. MVVM and Data Binding
  10. Navigation
  11. Platform Specific Services and Dependency Injection
  12. User Interface
  13. API Data Access
  14. Authentication
  15. Testing
  16. App Monitoring
  17. Other Books You May Enjoy