O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Cross-platform UI Development with Xamarin.Forms

Book Description

Create a fully operating application and deploy it to major mobile platforms using Xamarin.Forms

About This Book

  • Create standard user interfaces on Windows Mobile, Android, and iOS and then make those interfaces look good with ease
  • Design a full-blown application in very little time with just about the entire code being shared
  • Learn how to access platform-specific features and still have the same core code with this handy guide

Who This Book Is For

This book is intended for mobile software developers who are fed up with having three different code sets for the same application. If you want to put your code on all mobile platforms with minimum fuss, and just want to develop but haven’t got the time to be digging too far into a particular platform, this is the book for you. Basic knowledge of C# is assumed.

What You Will Learn

  • Create a responsive UI, modified to suit the target platform
  • Understand the basics of designing an application, and the considerations needed for target platforms
  • Construct a complete app using a single codebase
  • Develop attractive user interfaces
  • Bind information to the code behind to generate a reactive application
  • Design an effective portable class library (PCL)
  • Include a Windows Mobile application within your standard Xamarin.Forms application
  • Extend your applications using the Xamarin.Forms Labs library

In Detail

Xamarin is an IDE used for the development of native iOS, Android, and Windows, and cross-platform mobile applications in C#. For the mobile developer, that means learning three different languages to create the same application. Even if you use the Xamarin toolchain, you still need to work with three different user interface construction sets. Xamarin is essentially a container in which developers can write any application in C# and use the Xamarin compiler to package and deploy on Android, iOS, or Windows platforms. To top this, Xamarin.Forms plays the role of a single codebase for mobile applications.

This book will show you, with fully-coded examples, how to use both the Xamarin toolchain and the Xamarin.Forms library to code once for the three platforms. It goes from the concept and design of a mobile messenger application to its execution. You will be introduced to Messenger—the messaging app—which includes key features such as push notifications, UI, maps, databases, and web services. Next, you will learn to plan the UI using Xamarin.Forms for cross-mobile platform development, and move on to creating custom buttons, extending the UI, and connecting to social sites such as Facebook and Twitter.

You will also learn about the limitations of PCL libraries and how they make coding easier. This will be followed by the creation of a SQLite database and a database manager, and the SQLite database’s reflection within the database manager. You will then be taken through the use of hardware features with ample coverage of iOS, Android, and Windows Mobile. Finally, the book will conclude by introducing common strategies that allow you to create applications that “just work” without having to reinvent the wheel each time.

Style and approach

A fun and informal approach to creating a mobile application using the most up-to-date cross-platform approach. Each coding chapter includes fully working code examples available for download from the Packt Publishing website.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Cross-platform UI Development with Xamarin.Forms
    1. Table of Contents
    2. Cross-platform UI Development with Xamarin.Forms
    3. Credits
    4. About the Author
    5. Acknowledgments
    6. About the Reviewers
    7. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    8. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    9. 1. In the Beginning…
      1. Application planning
        1. The design templates
        2. A messenger – a shopping list of features
          1. Speech to text and text to speech
        3. Other considerations
        4. Cross-platform considerations
          1. Language considerations
            1. Online translation services
        5. The internal data structure
          1. The messenger data structure
          2. The message
          3. Contacts
        6. Backend storage
        7. Making sure the messages get through
          1. Setting up for Android
            1. Setting up for iOS
            2. Creating your iOS profile
          2. Creating and configuring for push notifications
          3. Exporting the certificate for Azure
        8. Adding packages to your application
      2. Summary
    10. 2. Let's Get the Party Started
      1. What is Xamarin Forms?
        1. Pages
        2. Layouts
        4. Cells
        5. So, how does this all work?
          1. Storing information
      2. Instantiating Xamarin Forms within an app
        1. Android
        2. iOS
        3. Windows Phone
        4. Adding Windows Phone to your project
          1. Import from Mac into Visual Studio
          2. Adding the libraries
          3. Modifying the XAML code
      3. Dependency injection
        1. Inversion of Control?
        2. Implementing DI
      4. Customizing the UI
      5. Gestures, maps, and WebViews
        1. Gestures
          1. Adding a gesture recognizer
        2. WebViews
          1. Displaying a web page
          2. Displaying a generated web page
          3. Displaying a web page from a file
          4. Displaying a web page from a file – iOS
          5. Displaying a web page from a file – Android
          6. Displaying a web page from a file – Windows Phone
        3. Maps
          1. Setting up on iOS
          2. Setting up on Android
          3. Setting up on Windows Phone
        4. Adding a map
          1. Adding a zoom facility to a map
          2. Sticking a pin in it
      6. Summary
    11. 3. Making It Look Pretty and Logging In
      1. Introducing the standard UI login experience
        1. Abstract this, abstract that
          1. When size really does matter
          2. Let the device help you
          3. That button, though…
        2. The custom renderer
          1. Creating a custom renderer
          2. Customizing for Android
          3. Implementing on iOS
          4. A complex UI example
            1. An Android custom renderer
            2. An iOS custom renderer
        3. Implementing a login screen
          1. UI positioning
          2. Making it look good
            1. Using EventTrigger
          3. Further trigger enhancements
      2. Summary
    12. 4. Making Your Application Portable
      1. PCLs – the pros and cons
        1. If you don't have the full .NET base, what is the point of having a PCL?
        2. If you don't have access to parts of libraries, such as hardware access, how can they be implemented?
        3. Is there a processor speed hit?
      2. Writing your PCL library
        1. Examining the code
        2. The old code
        3. The new code
      3. Other options for PCL
      4. Summary
    13. 5. Data, Generics, and Making Sense of Information
      1. A history lesson
      2. .NET generics
        1. Restrictions on using a generic class
        2. The generic methods
        3. Modern linked lists
      3. LINQ me up baby – yeah!
        1. Jumping into the code
        2. Wait, hold on, that's wrong!
      4. The database helper class
        1. Generic types within the helper class
          1. Interfacing with the data classes
          2. Creating the helper class
            1. The database connection
            2. Setting up the database
            3. Data, data everywhere…
            4. The code bit
        2. Getting funky with Func
        3. Getting data back out
      5. Too much information!
        1. Getting Linq'd
        2. Finding data with LINQ
          1. Where
          2. First and FirstOrDefault
          3. Single and SingleOrDefault
          4. Select
          5. As a loop
          6. Inline
          7. SelectMany
          8. Last and LastOrDefault
        3. Ordering data
          1. OrderBy
          2. t=>t?
          3. OrderByDescending
        4. ToList, ToArray, ToDictionary, and ToLookup
        5. Filters and mutables within LINQ
        6. Skipping and taking data
          1. Skip and SkipWhile
          2. Take and TakeWhile
        7. Other LINQ methods
          1. Aggregate
          2. All
          3. Any
          4. SequenceEqual
      6. Summary
    14. 6. A View to a Kill
      1. Touch-a-touch-a-touch me
        1. Wanted… dead or alive
          1. Knowing me, knowing you
        2. Start all over
          1. Little boxes made of ticky-tacky
          2. Can we fix it? Yes we can!
        3. Back to basics
        4. Uncle Tommy, there's more at the door…
          1. Size matters
          2. And then she cried out – more
          3. Move over Beethoven
        5. Now you see me, now you don't
          1. Here we go again…
          2. Problem 1 – talking with the PCL
          3. Problem 2 – the Android approach
          4. Problem 3 – the iOS approach
          5. Problem 4 – Windows Phone
        6. She's a killer…
        7. Modifying on Windows Phone
        8. Let's see what's left
      2. Summary
      3. References
    15. 7. Connect Me to Your Other Services
      1. Let's take a REST
        1. POST and GET
          1. So far, it's easy
          2. Transferring JSON to something we can use
          3. It's a bit messy, isn't it?
        2. The practicalities
        3. Let's make a start
          1. Using Json.NET
          2. Which is better?
      2. The Windows Communication Framework
        1. Using the web service
          1. Adding a web reference
      3. Summary
      4. References
    16. 8. What a Bind!
      1. A bit of a history lesson
      2. Binding the mobile arena
        1. A simple binding project
          1. Let's step it up a notch
          2. LINQ, anyone?
          3. How to perform the binding
          4. Can we do anything else with the bindings?
          5. IValueConverter
          6. Setting the horizontal position
            1. Any other alternatives?
        2. To the next level, we will go!
          1. Android
          2. Extension methods
          3. The text width and height
          4. Taking into account the text typeface
          5. Converting a drawable image to a bitmap image
        3. Adding to your list
          1. Simulating to add a message
      3. Summary
    17. 9. Addressing the Issue
      1. Addressing issues
        1. The address storage class
        2. Accessing the internal address book
          1. The native implementation
          2. The native implementation for Android
          3. The native implementation for iOS
        3. Using the Xamarin mobile component
          1. Installing through the component store
          2. Installing through NuGet
          3. Mixing components and packages
          4. Errors
          5. Xamarin Mobile
        4. Viewing the address book
          1. What is WeakReference?
          2. Using BindingContext
        5. Weak reference versus BindingContext
      2. Summary
    18. 10. This is the World Calling…
      1. Using GPS and push notifications
        1. Setting up the iOS provisioning profile
          1. Setting up push notifications in the app
        2. Setting up Android for push notifications
          1. Setting up the Android push notifications in the app
          2. Additional permissions
          3. The broadcast receiver
          4. The Android service
            1. The Register action
            2. The Unregister action
            3. Listening for a return
            4. Receive
          5. Handling the actions in code
        3. Handling the messages
        4. What about GPS and maps?
          1. Why bother?
          2. Adding the geolocation events to the code
          3. Adding the geolocation events to Android
          4. Adding the geolocation events to iOS
          5. In the PCL
          6. Creating the Xamarin Forms map
          7. Adding the map
          8. Map types and pins
        5. Two caveats to be aware of
          1. Updating the UI very often
          2. Deregistering the listener
      2. Summary
    19. 11. A Portable Settings Class
      1. The native platform storage
        1. The iOS native platform storage
        2. The Android native platform storage
        3. The Windows Phone native platform storage
      2. Constructing a persistent and cross-platform settings system
        1. Creating the initial data
        2. Creating the implementation of the IUserSettings interface – Android
          1. Saving preferences
          2. Loading preferences
        3. Creating the implementation of the IUserSettings interface – iOS
        4. Creating the implementation of the IUserSettings interface – Windows Phone
      3. Is there an alternative?
        1. The XML-based solution
          1. The GetSet class
          2. The UserData class
          3. The serializer class
        2. The SQLite solution
      4. Summary
    20. 12. Xamarin Forms Labs
      1. What is Xamarin Forms Labs?
        1. Installing Xamarin Forms Labs in your project
        2. Checkboxes
          1. iOS
          2. Android
          3. Windows Phone
          4. In the PCL
        3. The resolver
        4. Rolling your own
        5. PCL
          1. The magical type T
          2. The devil is in the detail
          3. Should it stay or should it go?
        6. The general UI considerations
          1. What have we currently got?
            1. Marrying the interface to the UI
            2. The web view
            3. List views
          2. What else is there to add?
          3. Generating the UI
        7. Platform-specific considerations
        8. Let's make a start – Android
        9. iOS
        10. Windows Phone
        11. Creating the library
      2. Summary
    21. 13. Social Media into the Mix
      1. Connect me up, Scotty
      2. Setting up the event system and interface
      3. Setting up your Android code
        1. Broadcast whatcha-ma-call-it?
        2. Setting up the interface
        3. Listening for connectivity on Windows Phone
        4. Adding a notification
      4. Setting up iOS
      5. Setting up Windows Phone
      6. Adding social media to your app
        1. The difference between OAuth and OAuth2
        2. Implementing OAuth
        3. Installing Xamarin.Auth from NuGet
      7. OAuth1 for Twitter
        1. Let's jump on
        2. Authentication in the application
      8. Using OAuth2
      9. The user interface
      10. Summary
      11. References
    22. 14. Bringing It All Together
      1. Setting up Azure
        1. Signing up
          1. Adding data
        2. Adding storage
      2. Setting up the application
        1. Adding Azure
          1. Adding the Azure login API
        2. Putting some meat on the bones
          1. Sign up and log in with the same code
          2. The automatic login
          3. The required data to sign up
          4. The data organization
        3. Wrapping up the login and base settings
      3. The Azure Storage
        1. Storing data
          1. Setting and storing
      4. The messenger application
        1. What about displaying the messages?
          1. The message composition
          2. Speech to text
          3. The message composition
          4. Lazy image loading
        2. Contacts
          1. Using social media
          2. The direct invitation
          3. Are the words "I have a cunning plan" marching with ill-deserved confidence in the direction of this?
        3. Displaying and storing the attachments
          1. Using file streams to save and load
      5. And that's it!
      6. Summary
    23. Index