your first xamarin.forms app

36
Your First Xamarin.Forms App Developer Evangelist @conceptdev Craig Dunn

Upload: craig-dunn

Post on 29-Nov-2014

814 views

Category:

Technology


2 download

DESCRIPTION

Slides for my session at Xamarin Evolve 2014. Code for the Todo app is here https://github.com/xamarin/xamarin-forms-samples/tree/master/Todo/PCL (there is a XAML version here) https://github.com/conceptdev/xamarin-forms-samples/tree/master/TodoXaml And the 8ball app is mentioned here https://github.com/xamarin/mini-hacks/tree/master/Xamarin.Forms

TRANSCRIPT

Page 1: Your First Xamarin.Forms App

Your First Xamarin.Forms

App

Developer Evangelist@conceptdev

Craig Dunn

Page 2: Your First Xamarin.Forms App

Forget Everything!

Page 3: Your First Xamarin.Forms App

Meet Xamarin.FormsBuild native UIs for iOS, Android and Windows Phone

from a single, shared C# codebase.

Page 4: Your First Xamarin.Forms App

Meet Xamarin.FormsBuild native UIs for iOS, Android and Windows Phone

from a single, shared C# codebase.

Page 5: Your First Xamarin.Forms App

Meet Xamarin.FormsBuild native UIs for iOS, Android and Windows Phone

from a single, shared C# codebase.

Page 6: Your First Xamarin.Forms App

Meet Xamarin.Forms

public class HelloWorld : ContentPage{ public HelloWorld () { var button = new Button { Text = "Say Hello" } ; button.Clicked += SayHelloClicked; Content = new StackLayout { new Label { Text = "Hello World" } , button }; } ! void SayHelloClicked (object s, EventArgs e) { // do something } }

<?xml version="1.0" encoding="UTF-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" x:Class="HelloForms.HelloWorld"> <StackLayout> <Label Text="Hello World" /> <Button Text="Say Hello" OnClick="SayHelloClicked" /> </StackLayout> </ContentPage>

!public partial class HelloWorld : ContentPage { public HelloWorld () { InitializeComponent (); } void SayHelloClicked (object s, EventArgs e) { // do something } }

C# XAML

Page 7: Your First Xamarin.Forms App

Your first Xamarin.Forms app

■ File > New Project ■ Design a screen ■ Add some code ■ Run on iOS, Android,

& Windows Phone

Page 8: Your First Xamarin.Forms App

■ Write shared C# code ■ Database, Web Services ■ Business Logic

■ Build native UIs ■ UIKit & Storyboards on iOS ■ AXML for Android ■ XAML for Windows Phone

■ Share 60-‐80% of the code

Using the native UI SDKs

Traditional Xamarin Development

Shared App LogicShared C# App Logic

UIKit Layout XAML

Page 9: Your First Xamarin.Forms App

Shared App Logic

UIKit Layout XAML

Shared C# User Interface CodeShared C# App Logic

■ Use the same strategies for sharing ■ Database, Web Services ■ Business Logic

■ Build the UI with a single shared codebase

!

■ Share 99% of the code

Using Xamarin.FormsTo build the User Interface

Page 10: Your First Xamarin.Forms App

■ Native look and feel ■ Code sharing/re-‐use ■ Access to native SDKs using Custom

Renderers and DependencyService ■ Camera, accelerometer, GPS, ■ NFC & more on Android ■ PassKit & more on iOS ■ Tiles & more on Windows Phone

Benefits

Using Xamarin.Forms

Shared App Logic

Shared C# User Interface Code

Shared C# App Logic

Page 11: Your First Xamarin.Forms App

How Xamarin.Forms works

■ Anatomy of a Xamarin.Forms solution ■ Controls ■ Layouts ■ Pages

Page 12: Your First Xamarin.Forms App

How Xamarin.Forms worksAnatomy of a Xamarin.Forms Solution■ PCL or Shared Project

Page 13: Your First Xamarin.Forms App

How Xamarin.Forms worksAnatomy of a Xamarin.Forms Solution■ PCL or Shared Project ■ NuGet Package

Page 14: Your First Xamarin.Forms App

How Xamarin.Forms worksAnatomy of a Xamarin.Forms Solution■ PCL or Shared Project ■ NuGet Package ■ App.cs

Page 15: Your First Xamarin.Forms App

How Xamarin.Forms worksAnatomy of a Xamarin.Forms Solution■ PCL or Shared Project ■ NuGet Package ■ App.cs ■ Android app

Page 16: Your First Xamarin.Forms App

How Xamarin.Forms worksAnatomy of a Xamarin.Forms Solution■ PCL or Shared Project ■ NuGet Package ■ App.cs ■ Android app ■ iOS app

Page 17: Your First Xamarin.Forms App

How Xamarin.Forms worksAnatomy of a Xamarin.Forms Solution■ PCL or Shared Project ■ NuGet Package ■ App.cs ■ Android app ■ iOS app ■ Windows Phone app

Page 18: Your First Xamarin.Forms App

How Xamarin.Forms works

var hello = new Label { Text = "Hello World" }; var ok = new Button { Text = "OK" };ok.Clicked += async (sender, e) => { await DisplayAlert("OK", "You said OK", "Done");};

!!<Label Text="Hello World" /> <Button Text="OK" OnClick="OkClicked" />

Controls

image: Balsamiq

Page 19: Your First Xamarin.Forms App

How Xamarin.Forms worksLayoutsvar layout = new StackLayout { Orientation = StackOrientation.Vertical, Children = { hello, ok } };

<StackLayout Orientation="Vertical"> <Label x:Name="hello" Text="Hello World" /> <Button x:Name="ok" Text="OK" OnClick="OkClicked"/></StackLayout>

image: Balsamiq

Page 20: Your First Xamarin.Forms App

How Xamarin.Forms worksPagespublic class HelloWorld : ContentPage{ public HelloWorld () { var button = new Button { Text = "OK" } ; button.Clicked += SayHelloClicked; Content = new StackLayout { new Label { Text = "Hello World" } , button }; } void SayHelloClicked (object s, EventArgs e) { // display an alert } } !<?xml version="1.0" encoding="UTF-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009 x:Class="HelloForms.HelloWorld"> <StackLayout Orientation="Vertical"> <Label x:Name="hello" Text="Hello World" /> <Button x:Name="ok" Text="OK" OnClick="OkClicked" /> </StackLayout> </ContentPage> image: Balsamiq

Page 21: Your First Xamarin.Forms App

Platform RenderersTaking Xamarin.Forms UI to the people (﴾devices)﴿

?

??

Page 22: Your First Xamarin.Forms App

Platform RenderersTaking Xamarin.Forms UI to the people

Page 23: Your First Xamarin.Forms App

Xamarin.Forms brings common UX to everyone

iOS does not have a native control for the iPhone, however Xamarin.Forms uses UISplitViewController on iPad.

Android has a native 'drawer' control which Xamarin.Forms uses.

Windows Phone doesn’t have a comparable UI metaphor, so Xamarin.Forms provides an implementation.

MasterDetailPage

Page 24: Your First Xamarin.Forms App

Xamarin.Forms brings common UX to everyone

iOS has the UINavigationController which Xamarin.Forms leverages.

Android has the navigation stack built in, but Xamarin.Forms adds the automatic 'back' button for API consistency.

Windows Phone also has a back-‐stack with hardware button, so Xamarin.Forms takes advantage of that.

NavigationPage

Page 25: Your First Xamarin.Forms App

140 new Controls!Welcome to our new Xamarin.Forms partners

http://blog.xamarin.com/enterprise-‐component-‐vendors-‐join-‐xamarin.forms-‐ecosystem/

Page 26: Your First Xamarin.Forms App

Your second Xamarin.Forms app

■ File > New Project ■ Design some screens ■ Add some code ■ Run on iOS, Android,

& Windows Phone

Page 27: Your First Xamarin.Forms App

Dependency ServiceEasily call into platform-‐specific code■ In the common code

■ Code to an Interface

public interface ITextToSpeech{ void Speak (string text);} !!DependencyService.Get<ITextToSpeech>().Speak("Hello from Xamarin Forms");

!!!

Page 28: Your First Xamarin.Forms App

Dependency ServiceEasily call into platform-‐specific code■ In the common code

■ Code to an Interface ■ Use DependencyService

public interface ITextToSpeech{ void Speak (string text);} !!DependencyService.Get<ITextToSpeech>().Speak("Hello from Xamarin Forms");

!!!

Page 29: Your First Xamarin.Forms App

Dependency ServiceEasily call into platform-‐specific code■ In the common code

■ Code to an Interface ■ Use DependencyService

■ For each platform ■ implement the Interface

[assembly: Xamarin.Forms.Dependency (typeof (Speech))] public class Speech : ITextToSpeech{ public Speech () { } public void Speak (string text) { var speechSynthesizer = new AVSpeechSynthesizer (); var speechUtterance = new AVSpeechUtterance (text) { Rate = AVSpeechUtterance.MaximumSpeechRate/4, Voice = AVSpeechSynthesisVoice.FromLanguage ("en-US"), Volume = 0.5f, PitchMultiplier = 1.0f } ; speechSynthesizer.SpeakUtterance (speechUtterance); } }

Page 30: Your First Xamarin.Forms App

Dependency ServiceEasily call into platform-‐specific code■ In the common code

■ Code to an Interface ■ Use DependencyService

■ For each platform ■ implement the Interface ■ use Dependency

attribute on the assembly

[assembly: Xamarin.Forms.Dependency (typeof (Speech))] public class Speech : ITextToSpeech{ public Speech () { } public void Speak (string text) { var speechSynthesizer = new AVSpeechSynthesizer (); var speechUtterance = new AVSpeechUtterance (text) { Rate = AVSpeechUtterance.MaximumSpeechRate/4, Voice = AVSpeechSynthesisVoice.FromLanguage ("en-US"), Volume = 0.5f, PitchMultiplier = 1.0f } ; speechSynthesizer.SpeakUtterance (speechUtterance); } }

Page 31: Your First Xamarin.Forms App

Data BindingSync views and models■ Enables MVVM-‐style development ■ SetBinding in C# ■ {Binding} in XAML ■ also supports the Command pattern

Page 32: Your First Xamarin.Forms App

Messaging CenterLoosely-‐coupled publish and subscribe

Subscriber PublisherMessaging Center

1. Register

3. Receive

2. Send Message

Page 33: Your First Xamarin.Forms App

Custom RenderersExtend or Create Xamarin.Forms Controls■ Subclass the built-‐in Platform Renderers

!

■ Build your own Xamarin.Formscontrol and renderers(﴾eg. OxyPlot)﴿

!

■ Attend Jason’s Extending Xamarin.Forms talkon Thursday

Page 34: Your First Xamarin.Forms App

Further Reading…

■ developer.xamarin.com ■ forums.xamarin.com ■ Creating Mobile Apps with

Xamarin.Forms -‐ Charles PetzoldPREVIEW EDITION available for all Evolve 2014 attendees

Page 35: Your First Xamarin.Forms App

Today • Building cross platform applications -‐ Laurent Bugnion Tomorrow • XAML for Xamarin.Forms -‐ Charles Petzold • Extending Xamarin.Forms -‐ Jason Smith Friday • Xamarin.Forms is Cooler Than You Think -‐ Charles Petzold

!

Mini-‐hacks • Visit the Darwin Lounge

What’s next?

Page 36: Your First Xamarin.Forms App

Your First Xamarin.Forms

App

Craig Dunn@conceptdev

[email protected]