Xamarin.Forms
Total Page:16
File Type:pdf, Size:1020Kb
Xamarin.Forms #xamarin.fo rms Table of Contents About 1 Chapter 1: Getting started with Xamarin.Forms 2 Remarks 2 Versions 2 Examples 3 Installation (Visual Studio) 3 Xamarin Plugin for Visual Studio 3 Xamarin.Forms 4 Hello World Xamarin Forms: Visual Studio 5 Step 1: Creating a new Project. 5 Step 2: Investigating the sample 6 Step 3: Launching the application 7 Chapter 2: Accessing native features with DependencyService 8 Remarks 8 Examples 8 Implementing text-to-speech 8 iOS Implementation 9 Android Implementation 10 Windows Phone Implementation 11 Implementing in Shared Code 12 Getting Application and Device OS Version Numbers - Android & iOS - PCL 12 Chapter 3: AppSettings Reader in Xamarin.Forms 15 Examples 15 Reading app.config file in a Xamarin.Forms Xaml project 15 Chapter 4: BDD Unit Testing in Xamarin.Forms 17 Remarks 17 Examples 17 Simple Specflow to test commands and navigation with NUnit Test Runner 17 Why do we need this? 17 Usage: 17 Advanced Usage for MVVM 19 Chapter 5: Caching 21 Examples 21 Caching using Akavache 21 About Akavache 21 Recommendations for Xamarin 21 Simple example 21 Error handling 22 Chapter 6: CarouselView - Pre-release version 23 Remarks 23 Examples 23 Import CarouselView 23 Import CarouselView into a XAML Page 24 The basics 24 Creating bindable source 24 DataTemplates 24 Chapter 7: Contact Picker - Xamarin Forms (Android and iOS) 26 Remarks 26 Examples 26 contact_picker.cs 26 MyPage.cs 26 ChooseContactPicker.cs 27 ChooseContactActivity.cs 27 MainActivity.cs 29 ChooseContactRenderer.cs 29 Chapter 8: Creating custom controls 32 Examples 32 Create an Xamarin Forms custom input control (no native required) 32 Label with bindable collection of Spans 34 Creating a custom Entry control with a MaxLength property 36 Chapter 9: Creating custom controls 38 Introduction 38 Examples 38 Implementing a CheckBox Control 38 Creating the Custom Control 38 Consuming the Custom Control 39 Creating the Custom Renderer on each Platform 39 Creating the Custom Renderer for Android 40 Creating the Custom Renderer for iOS 41 Chapter 10: Creating custom controls 45 Examples 45 Creating custom Button 45 Chapter 11: Custom Fonts in Styles 47 Remarks 47 Examples 47 Accessing custom Fonts in Syles 47 Chapter 12: Custom Renderers 50 Examples 50 Custom renderer for ListView 50 Custom Renderer for BoxView 52 Accessing renderer from a native project 55 Rounded label with a custom renderer for Frame (PCL & iOS parts) 56 Rounded BoxView with selectable background color 57 Chapter 13: Data Binding 60 Remarks 60 Possible Exceptions 60 System.ArrayTypeMismatchException: Attempted to access an element as a type incompatible w 60 System.ArgumentException: Object of type 'Xamarin.Forms.Binding' cannot be converted to ty 60 The Picker.Items Property Is Not Bindable 60 Examples 61 Basic Binding to ViewModel 61 Chapter 14: Dependency Services 63 Remarks 63 Examples 63 Access Camera and Gallery. 63 Chapter 15: DependencyService 64 Remarks 64 Examples 64 Interface 64 iOS implementation 64 Shared code 65 Android implementation 66 Chapter 16: Display Alert 68 Examples 68 DisplayAlert 68 Alert Example with only one button and action 69 Chapter 17: Effects 70 Introduction 70 Examples 70 Adding platform specific Effect for an Entry control 70 Chapter 18: Exception handling 75 Examples 75 One way to report about exceptions on iOS 75 Chapter 19: Generic Xamarin.Forms app lifecycle? Platform-dependant! 78 Examples 78 Xamarin.Forms lifecycle is not the actual app lifecycle but a cross-platform representatio 78 Chapter 20: Gestures 80 Examples 80 Make an Image tappable by adding a TapGestureRecognizer 80 Zoom an Image with the Pinch gesture 80 Show all of the zoomed Image content with the PanGestureRecognizer 81 Place a pin where the user touched the screen with MR.Gestures 81 Chapter 21: MessagingCenter 83 Introduction 83 Examples 83 Simple example 83 Passing arguments 84 Unsubscribing 84 Chapter 22: Navigation in Xamarin.Forms 85 Examples 85 NavigationPage flow 85 NavigationPage flow with XAML 86 Hierarchical navigation with XAML 87 Pushing new pages 87 Page1.xaml 88 Page1.xaml.cs 88 Page2.xaml 88 Page2.xaml.cs 88 Popping pages 89 Page3.xaml 89 Page3.xaml.cs 89 Modal navigation with XAML 89 Full screen modals 90 Alerts/Confirmations and Notifications 90 ActionSheets 90 Master Detail Root Page 90 Master Detail Navigation 91 Chapter 23: Navigation in Xamarin.Forms 92 Remarks 92 Examples 92 Using INavigation from view model 92 Chapter 24: OAuth2 96 Examples 96 Authentication by using Plugin 96 Chapter 25: Platform specific visual adjustments 98 Examples 98 Idiom adjustments 98 Platform adjustments 98 Using styles 99 Using custom views 99 Chapter 26: Platform-specific behaviour 101 Remarks 101 Examples 101 Removing icon in navigation header in Anroid 101 Make label's font size smaller in iOS 102 Chapter 27: Push Notifications 104 Remarks 104 Examples 104 Push notifications for iOS with Azure 104 Push notifications for Android with Azure 107 Push notifications for Windows Phone with Azure 110 Chapter 28: Push Notifications 112 Remarks 112 AWS Simple Notification Service Lingo: 112 Generic Push Notification Lingo: 112 Examples 112 iOS Example 112 Chapter 29: SQL Database and API in Xamarin Forms. 114 Remarks 114 Examples 114 Create API using SQL database and implement in Xamarin forms, 114 Chapter 30: Triggers & Behaviours 115 Examples 115 Xamarin Forms Trigger Example 115 Multi Triggers 116 Chapter 31: Unit Testing 118 Examples 118 Testing the view models 118 Before we start... 118 Business requirements 118 Common classes 119 Services 119 Building the ViewModel stub 120 How to create a LoginPageViewModel instance? 121 Tests 121 Writing tests 122 Business logic implementation 123 Chapter 32: Using ListViews 125 Introduction 125 Examples 125 Pull to Refresh in XAML and Code behind 125 Chapter 33: Why Xamarin Forms and When to use Xamarin Forms 126 Remarks 126 Examples 126 Why Xamarin Forms and When to use Xamarin Forms 126 Chapter 34: Working with local databases 128 Examples 128 Using SQLite.NET in a Shared Project 128 Working with local databases using xamarin.forms in visual studio 2015 130 Chapter 35: Working with Maps 140 Remarks 140 Examples 140 Adding a map in Xamarin.Forms (Xamarin Studio) 140 Maps Initialization 140 iOS project 140 Android project 140 Platform Configuration 141 iOS project 141 Android project 142 Adding a map 151 PCL project 151 Chapter 36: Xamarin Forms Layouts 153 Examples 153 ContentPresenter 153 ContentView 153 Frame 154 ScrollView 155 TemplatedView 156 AbsoluteLayout 157 Grid 160 RelativeLayout 162 StackLayout 163 Usage in XAML 164 Usage in code 164 Chapter 37: Xamarin Gesture 167 Examples 167 Tap Gesture 167 Chapter 38: Xamarin Gesture 168 Examples 168 Gesture Event 168 Chapter 39: Xamarin Plugin 171 Examples 171 Share Plugin 171 ExternalMaps 171 Geolocator Plugin 172 Media Plugin 174 Messaging Plugin 177 Permissions Plugin 179 Chapter 40: Xamarin Relative Layout 183 Remarks 183 Examples 183 Page with an simple label on the middle 183 Box after box 185 Chapter 41: Xamarin.Forms Cells 188 Examples 188 EntryCell 188 SwitchCell 188 TextCell 189 ImageCell 190 ViewCell 191 Chapter 42: Xamarin.Forms Page 193 Examples 193 TabbedPage 193 ContentPage 194 MasterDetailPage 195 Chapter 43: Xamarin.Forms Views 197 Examples 197 Button 197 DatePicker 198 Entry 199 Editor 200 Image 201 Label 202 Credits 204 About You can share this PDF with anyone you feel could benefit from it, downloaded the latest version from: xamarin-forms It is an unofficial and free Xamarin.Forms ebook created for educational purposes. All the content is extracted from Stack Overflow Documentation, which is written by many hardworking individuals at Stack Overflow. It is neither affiliated with Stack Overflow nor official Xamarin.Forms. The content is released under Creative Commons BY-SA, and the list of contributors to each chapter are provided in the credits section at the end of this book. Images may be copyright of their respective owners unless otherwise specified. All trademarks and registered trademarks are the property of their respective company owners. Use the content presented in this book at your own risk; it is not guaranteed to be correct nor accurate, please send your feedback and corrections to [email protected] https://riptutorial.com/ 1 Chapter 1: Getting started with Xamarin.Forms Remarks Xamarin.Forms makes it possible to create iOS, Android, and Windows apps with large amounts of shared code, including UI code or XAML UI markup. App pages and views are mapped to native controls on each platform, but can be customized to provide platform-specific UI or to access platform-specific features. Versions Version Release Date 2.3.1 2016-08-03 2.3.0-hotfix1 2016-06-29 2.3.0 2016-06-16 2.2.0-hotfix1 2016-05-30 2.2.0 2016-04-27 2.1.0 2016-03-13 2.0.1 2016-01-20 2.0.0 2015-11-17 1.5.1 2016-10-20 1.5.0 2016-09-25 1.4.4 2015-07-27 1.4.3 2015-06-30 1.4.2 2015-04-21 1.4.1 2015-03-30 1.4.0 2015-03-09 1.3.5 2015-03-02 https://riptutorial.com/ 2 Version Release Date 1.3.4 2015-02-17 1.3.3 2015-02-09 1.3.2 2015-02-03 1.3.1 2015-01-04 1.3.0 2014-12-24 1.2.3 2014-10-02 1.2.2 2014-07-30 1.2.1 2014-07-14 1.2.0 2014-07-11 1.1.1 2014-06-19 1.1.0 2014-06-12 1.0.1 2014-06-04 Examples Installation (Visual Studio) Xamarin.Forms is a cross-platform natively backed UI toolkit abstraction that allows developers to easily create user interfaces that can be shared across Android, iOS, Windows, and Windows Phone.