ebook img

Creating Mobile Apps with Xamarin.Forms Preview Edition 2 PDF

453 Pages·2015·15.1 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Creating Mobile Apps with Xamarin.Forms Preview Edition 2

Creating Mobile Apps with Xamarin.Forms 2 Cross-platform C# programming for iOS, Android, and Windows Phone CHARLES PETZOLD PREVIEW EDITION This excerpt provides early content from a book currently in development and is still in draft format. See additional notice below. PUBLISHED BY Microsoft Press A Division of Microsoft Corporation One Microsoft Way Redmond, Washington 98052-6399 Copyright © 2015 Xamarin, Inc. All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher. ISBN: 978-0-7356-9723-2 Microsoft Press books are available through booksellers and distributors worldwide. Please tell us what you think of this book at http://aka.ms/tellpress. This document is provided for informational purposes only and Microsoft makes no warranties, either express or implied, in this document. Information in this document, including URL and other Internet website references, is subject to change without notice. The entire risk of the use or the results from the use of this document remains with the user. This book expresses the author’s views and opinions. The information contained in this book is provided without any express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers, or distrib- utors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under cop- yright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation. Microsoft and the trademarks listed at http://www.microsoft.com/about/legal/en/us/IntellectualProperty/ Trade- marks/EN-US.aspx are trademarks of the Microsoft group of companies. All other marks are property of their re- spective owners. The example companies, organizations, products, domain names, email addresses, logos, people, places, and events depicted herein are fictitious. No association with any real company, organization, product, domain name, email address, logo, person, place, or event is intended or should be inferred. This book expresses the author’s views and opinions. The information contained in this book is provided without any express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers, or distrib- utors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book. Acquisitions and Project Editor: Devon Musgrave Editorial production: John Pierce, Flying Squirrel Press Cover illustration: Serena Zhang Table of contents Introduction ....................................................................................................................... viii Who should read this book ...................................................................................................................................................... viii Conventions and features in this book ................................................................................................................................ viii The 1st and 2nd Preview Editions ............................................................................................................................................... ix System requirements ..................................................................................................................................................................... ix Downloads: Code samples ............................................................................................................................................................ x Updating the code samples ..................................................................................................................................................... x Big changes coming for Windows Phone ........................................................................................................................ xi Acknowledgments .......................................................................................................................................................................... xi Free ebooks from Microsoft Press ........................................................................................................................................... xii We want to hear from you ......................................................................................................................................................... xii Chapter 1 How does Xamarin.Forms fit in? .............................................................. 1 Cross-platform mobile development ....................................................................................................................................... 2 The mobile landscape................................................................................................................................................................ 2 Problem 1: Different user-interface paradigms .............................................................................................................. 2 Problem 2: Different development environments ......................................................................................................... 3 Problem 3: Different programming interfaces ................................................................................................................ 3 Problem 4: Different programming languages ............................................................................................................... 3 The C# and .NET solution ............................................................................................................................................................. 4 A single language for all platforms ...................................................................................................................................... 5 Sharing code ................................................................................................................................................................................. 6 Introducing Xamarin.Forms ......................................................................................................................................................... 8 The Xamarin.Forms option ...................................................................................................................................................... 8 XAML support ............................................................................................................................................................................. 10 Platform specificity ................................................................................................................................................................... 12 A cross-platform panacea? .................................................................................................................................................... 12 Your development environment ............................................................................................................................................. 13 Table of contents iii Machines and IDEs ................................................................................................................................................................... 13 Devices and emulators ............................................................................................................................................................ 14 Installation ........................................................................................................................................................................................ 14 Creating an iOS app ................................................................................................................................................................. 15 Creating an Android app ....................................................................................................................................................... 15 Creating a Windows Phone app ......................................................................................................................................... 16 All ready? ...................................................................................................................................................................................... 16 Chapter 2 Anatomy of an app .................................................................................... 17 Say hello ............................................................................................................................................................................................ 17 Inside the files ................................................................................................................................................................................. 20 The iOS Project .......................................................................................................................................................................... 23 The Android project ................................................................................................................................................................. 23 The Windows Phone project ................................................................................................................................................ 24 Nothing special! ........................................................................................................................................................................ 25 PCL or SAP? ...................................................................................................................................................................................... 25 Labels for text .................................................................................................................................................................................. 27 1. Include padding on the page ......................................................................................................................................... 31 2. Include padding just for iOS (Shared Asset Project only) .................................................................................... 32 3. Include padding just for iOS (PCL or SAP) ................................................................................................................. 33 4. Center the label within the page ................................................................................................................................... 35 5. Center the text within the label...................................................................................................................................... 37 Chapter 3 Deeper into text .......................................................................................... 38 Wrapping paragraphs .................................................................................................................................................................. 38 Text and background colors ..................................................................................................................................................... 40 The Color structure ....................................................................................................................................................................... 42 Font sizes and attributes ............................................................................................................................................................. 46 Formatted text ................................................................................................................................................................................ 47 Chapter 4 Scrolling the stack ...................................................................................... 54 Stacks of views ................................................................................................................................................................................ 54 Scrolling content ............................................................................................................................................................................ 58 Table of contents iv The Expands option ...................................................................................................................................................................... 64 Frame and BoxView ...................................................................................................................................................................... 68 A ScrollView in a StackLayout? ................................................................................................................................................. 76 Chapter 5 Dealing with sizes ...................................................................................... 81 Pixels, points, dps, DIPs, and DIUs ........................................................................................................................................... 81 Metrical sizes .................................................................................................................................................................................... 88 Estimated font sizes ...................................................................................................................................................................... 89 Fitting text to available size ....................................................................................................................................................... 92 A fit-to-size clock ........................................................................................................................................................................... 96 Empirically fitting text .................................................................................................................................................................. 98 Chapter 6 Button clicks .............................................................................................. 103 Processing the click .................................................................................................................................................................... 103 Sharing button clicks ................................................................................................................................................................. 106 Anonymous event handlers .................................................................................................................................................... 109 Distinguishing views with IDs ................................................................................................................................................. 111 Saving transient data ................................................................................................................................................................. 114 Chapter 7 XAML vs. code ........................................................................................... 121 Properties and attributes ......................................................................................................................................................... 122 Property-element syntax.......................................................................................................................................................... 126 Adding a XAML page to your project ................................................................................................................................ 130 Platform specificity in the XAML file ................................................................................................................................... 135 The content property attribute ............................................................................................................................................. 139 Formatted text ............................................................................................................................................................................. 141 Chapter 8 Code and XAML in harmony ................................................................ 145 Passing arguments ..................................................................................................................................................................... 145 Constructors with arguments ............................................................................................................................................ 145 Can I call methods from XAML? ....................................................................................................................................... 148 The x:Name attribute................................................................................................................................................................. 150 Custom XAML-based views .................................................................................................................................................... 156 Events and handlers ................................................................................................................................................................... 160 Table of contents v Tap gestures...................................................................................................................................................................................163 Chapter 9 Platform-specific API calls .................................................................... 170 Preprocessing in the Shared Asset Project ........................................................................................................................170 Parallel classes and the Shared Asset Project ...................................................................................................................173 DependencyService and the Portable Class Library ......................................................................................................175 Platform-specific sound rendering .......................................................................................................................................179 Chapter 10 XAML markup extensions .................................................................. 186 The code infrastructure .............................................................................................................................................................186 Accessing static members ........................................................................................................................................................188 Resource dictionaries .................................................................................................................................................................194 StaticResource for most purposes ....................................................................................................................................195 A tree of dictionaries .............................................................................................................................................................201 DynamicResource for special purposes .........................................................................................................................205 Lesser-used markup extensions .............................................................................................................................................208 A custom markup extension ...................................................................................................................................................209 Chapter 11 The bindable infrastructure ............................................................... 214 The Xamarin.Forms class hierarchy ......................................................................................................................................215 A peek into BindableObject and BindableProperty ......................................................................................................221 Defining bindable properties ..................................................................................................................................................228 The generic Create method ................................................................................................................................................233 The read-only bindable property .....................................................................................................................................234 Chapter 12 Styles ......................................................................................................... 240 The basic Style ..............................................................................................................................................................................240 Styles in code.................................................................................................................................................................................246 Style inheritance ...........................................................................................................................................................................247 Implicit styles .................................................................................................................................................................................252 Dynamic styles ..............................................................................................................................................................................257 Device styles ..................................................................................................................................................................................264 Chapter 13 Bitmaps ..................................................................................................... 269 Platform-independent bitmaps .............................................................................................................................................270 Table of contents vi Fit and fill ................................................................................................................................................................................... 273 Embedded resources ............................................................................................................................................................ 275 More on sizing ......................................................................................................................................................................... 281 Browsing and waiting ........................................................................................................................................................... 292 Streaming bitmaps ..................................................................................................................................................................... 296 Accessing the streams .......................................................................................................................................................... 297 Generating bitmaps at run time ....................................................................................................................................... 299 Platform-specific bitmaps ........................................................................................................................................................ 303 Bitmap resolutions ................................................................................................................................................................. 305 Toolbars and their icons ...................................................................................................................................................... 312 Button images ......................................................................................................................................................................... 319 Chapter 14 Absolute layout ...................................................................................... 322 AbsoluteLayout in code ............................................................................................................................................................ 323 Attached bindable properties ................................................................................................................................................ 328 Proportional sizing and positioning .................................................................................................................................... 332 Working with proportional coordinates ............................................................................................................................ 334 AbsoluteLayout and XAML ..................................................................................................................................................... 339 Overlays .......................................................................................................................................................................................... 343 Some fun ........................................................................................................................................................................................ 346 Chapter 15 The interactive interface ...................................................................... 355 View overview............................................................................................................................................................................... 355 Slider and Stepper ...................................................................................................................................................................... 356 Slider basics .............................................................................................................................................................................. 356 Common pitfalls ..................................................................................................................................................................... 359 Slider color selection ............................................................................................................................................................. 361 The Stepper difference ......................................................................................................................................................... 365 Switch and CheckBox ................................................................................................................................................................ 368 Switch basics ............................................................................................................................................................................. 368 A traditional CheckBox......................................................................................................................................................... 370 Typing text ..................................................................................................................................................................................... 375 Table of contents vii Keyboard and focus ...............................................................................................................................................................376 Choosing the keyboard ........................................................................................................................................................377 Entry properties and events ................................................................................................................................................379 The Editor difference .............................................................................................................................................................385 The SearchBar ...........................................................................................................................................................................389 Date and time selection ............................................................................................................................................................394 The DatePicker .........................................................................................................................................................................395 The TimePicker (or is it a TimeSpanPicker?) ................................................................................................................398 Chapter 16 Data binding ........................................................................................... 402 Binding basics ...............................................................................................................................................................................402 Code and XAML ...........................................................................................................................................................................404 Source and BindingContext ....................................................................................................................................................407 The binding mode .......................................................................................................................................................................414 String formatting .........................................................................................................................................................................419 Why is it called “Path”?..............................................................................................................................................................422 Binding value converters ..........................................................................................................................................................425 Bindings and custom views .....................................................................................................................................................433 Introduction This is the second Preview Edition of a book about writing applications for Xamarin.Forms, the exciting new mobile development platform for iOS, Android, and Windows Phone unveiled by Xamarin in May 2014. Xamarin.Forms lets you write shared user-interface code in C# and XAML (the Extensible Applica- tion Markup Language) that maps to native controls on these three platforms. This book is a Preview Edition because it’s not complete. It has only 16 chapters and doesn’t cover some important topics. The final edition of the book will probably be published in the summer of 2015. All information about this book can be found on the book’s home page at: http://developer.xamarin.com/guides/cross-platform/xamarin-forms/creating-mobile-apps-xamarin-forms/ Who should read this book This book is for C# programmers who want to write applications for the three most popular mobile platforms—iOS, Android, and Windows Phone—with a single code base. Xamarin.Forms also has ap- plicability for those programmers who want eventually to use C# and the Xamarin.iOS and Xama- rin.Android libraries to target the native application programming interfaces (APIs) of these platforms. Xamarin.Forms can be a big help in getting programmers started with these platforms or in construct- ing a prototype or proof-of-concept application. This book assumes that you know C# and are familiar with the use of the .NET Framework. However, when I discuss some C# and .NET features that might be somewhat new to recent C# programmers, I adopt a somewhat slower pace. Conventions and features in this book This book has just a few typographical conventions:  All programming elements referenced in the text—including classes, methods, properties, varia- ble names, etc.—are shown in a monospaced font, such as the StackLayout class.  Items that appear in the user interface of Visual Studio or Xamarin Studio, or the applications discussed in these chapters, appear in boldface, such as the Add New Project dialog.  Application solutions and projects also appear in boldface, such as MonkeyTap. Introduction ix The 1st and 2nd Preview Editions This book is intended as a tutorial to learn Xamarin.Forms programming. It is not a replacement for the online API documentation, which can be found at the Xamarin.Forms Framework link on this page: http://api.xamarin.com/. The first Preview Edition of this book was published in October 2014 to coincide with the Xamarin Evolve conference. It contained six chapters but no coverage of XAML. This second Preview Edition was reconceived to contain shorter and more focused chapters. After the book establishes a solid foundation in code-only Xamarin.Forms programning, XAML is introduced in Chapter 7, and from that point on, XAML is used to define the user interface of most of the sample programs. This Preview Edition unfortunately ends without covering some essential topics. The most obvious missing topics include the Grid, collection views such as ListView, page navigation, triggers, behav- iors, and maps. However, it is our intention to publish chapters online just about as quickly as I can write them. New chapters will appear on the Xamarin webpage devoted to this book. System requirements This book assumes that you’ll be using Xamarin.Forms to write applications that simultaneously target all three supported mobile platforms—iOS, Android, and Windows Phone. However, it’s very likely that many readers will be targeting only one or two platforms in their Xamarin.Forms solutions. The plat- forms you target—and the Xamarin Platform package you purchase—govern your hardware and soft- ware requirements. For targeting iOS devices, you’ll need a Mac installed with Apple Xcode as well as the Xamarin Platform, which includes Xamarin Studio. For targeting Windows Phone, you’ll need Visual Studio 2012 or later (not an Express Edition) on a PC, and you’ll need to have installed the Xamarin Platform. However, you can also use Visual Studio on the PC to target iOS devices if the Mac with Xcode and the Xamarin Platform are accessible via WiFi. You can target Android devices from Visual Studio on the PC or from Xamarin Studio on either the PC or Mac. Chapter 1 has more details on the various configurations you can use and resources for additional information and support. My setup for creating this book consisted of a Microsoft Surface Pro 2 (with external monitor, keyboard, and mouse) installed with Visual Studio 2013 and the Xamarin Platform, connected by WiFi with a MacBook Pro installed with Xcode and the Xamarin Platform. All the screen shots in this book show an iPhone, an Android phone, and a Windows Phone in that order. The three devices shown in these screen shots reflect my setup and hardware:  The iPhone 6 simulator on the MacBook Pro running iOS 8.2

Description:
CHARLES PETZOLD. Cross-platform C# programming for iOS, Android, and Windows Phone. Creating. Mobile Apps with Xamarin.Forms. 2
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.