Top Banner
TUGA IT 2016 LISBON, PORTUGAL
46

Making Cross-Platform apps with Xamarin

Jan 28, 2018

Download

Software

Diogo Cardoso
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Making Cross-Platform apps with Xamarin

TUGA IT 2016LISBON, PORTUGAL

Page 2: Making Cross-Platform apps with Xamarin

THANK YOU TO OUR

SPONSORS

Page 3: Making Cross-Platform apps with Xamarin

THANK YOU TO OUR

TEAMANDRÉ BATISTA ANDRÉ MELANCIA ANDRÉ VALA ANTÓNIO LOURENÇO BRUNO LOPES

CLÁUDIO SILVA

RUI BASTOS

NIKO NEUGEBAUER

RUI REISRICARDO CABRAL

NUNO CANCELO PAULO MATOS PEDRO SIMÕES

SANDRA MORGADO SANDRO PEREIRA

Page 4: Making Cross-Platform apps with Xamarin

MAKING CROSS-PLATFORM APPS WITH XAMARIN19/5/2016

Page 5: Making Cross-Platform apps with Xamarin

Ana Correia Diogo Cardoso

• Mobile Developer at ThingPink

• Developer at ImaginationOverflow

@[email protected]

• Indie App & Game Developer

• Teacher at ISEL

• Freelancer

@[email protected]

www.imaginationoverflow.com

Page 6: Making Cross-Platform apps with Xamarin

Summary

• Introduction to Xamarin

• Brief introduction to Android, iOS and Windows

• MVVM

• Portable class libraries and shared projects

• OAuth recap?

• Lifecycle

• Localisation

• Where to go next

2

1

3 4 5

6 7

8 9

10 11 12

Challenges

Page 7: Making Cross-Platform apps with Xamarin

Android iOS Windows 10Mac

Xamarin.Android Xamarn.iOS Xamarin.MacUniversal Windows Apps

Platform

Mono .NET .NET

C# Portable / Shared code

Android AXML

Android iOS Windows 10Mac

Xamarin.Android Xamarn.iOS Xamarin.MacUniversal Windows Apps

Platform

Mono .NET .NET

C# Portable / Shared code

C# code behind

iOS Storyboards

C# code behind

Mac XIB

C# code behind

Windows Xaml

C# code behind

Page 8: Making Cross-Platform apps with Xamarin

• LINQ

• Task Parallel Library

• Async / Await

• Generics*

• C# 6 features

• Etc

• Android 6.0

• API Level 23

• Android Wear

• Android Player

• 100% API Coverage

• iOS 9

• Apple Watch

• Develop iOS apps in

Windows

• Mac Agent

• 100% API Coverage

C# Android iOS

Xamarin - Features

Page 9: Making Cross-Platform apps with Xamarin

Xamarin - Native Apps

Android

• C# compiled to IL

• Deployed with MonoVM

• JIT

• Runs side by side with ART

• Interacts with native types with

JNI

iOS

• Compiled ahead-of-time

• 100% machine code

Page 10: Making Cross-Platform apps with Xamarin

Xamarin - Development

Visual Studio Xamarin Studio

• Windows 10

• Windows Phone

• Android

• iOS*

• Android

• iOS*

Page 11: Making Cross-Platform apps with Xamarin

Xamarin - Forms

• App 100% portable.

• Cross Platform user

interface layouts

• Ability to mix native

controls

• Available for Android,

iOS and Windows

Page 12: Making Cross-Platform apps with Xamarin

C# Async

async Task<string> GetHtmlAsync() {

var client = new HttpClient();

var html = await client.GetStringAsync("http://tugait.pt");

return html;

}

void GetHtml(Action<string> callback, Action<Exception> onError){

var client = new HttpClient();

client.BeginGetString("http://tugait.pt", (ar) => {

try {

var html = client.EndGetString(ar);

callback(html);

}

catch (Exception e) {

onError(e);

}

});

}

APM

Async

Page 13: Making Cross-Platform apps with Xamarin

Android iOS

Windows

Page 14: Making Cross-Platform apps with Xamarin

Purpose

• Bare Metal/Vanilla approach

• Basic Android, iOS and Windows understanding

• When and how should you make portable code

• App building blocks

Architecture

Storage

Localisation

Loading

Exception Handling

Code reusability

Lifecycle

Page 15: Making Cross-Platform apps with Xamarin

Challenge #1

Page 16: Making Cross-Platform apps with Xamarin

App Architecture

ViewModel

Controller

NotifyUser

Action

Updates

ViewModel

ViewModel

Interacts CommandsData

Binding

Model View Controller Model View ViewModel

Page 17: Making Cross-Platform apps with Xamarin

MVC

• Views call controller operations

• Controllers update the model or call its operations

• Controller notifies view

Android – callbacks, async tasks, etc

iOS – callbacks, delegates, etc

ViewModel

Controller

NotifyUser

Action

Updates

Page 18: Making Cross-Platform apps with Xamarin

MVVM

• Views binds to ViewModels

• Views invoke ViewModel commands

• ViewModels update the model or

invoke its operations

• ViewModels update its properties

• Views reacts to the change in binded

properties and update itself

ViewModel

ViewModel

Interacts CommandsData

Binding

Page 19: Making Cross-Platform apps with Xamarin

MVC vs MVVM

MVC

• Controllers know their views

Ctor passage - new Controller(this)

Direct parametrization

controller.DoStuff(this)

Callbacks

• Is not data oriented

• Its goal is to separate concerns

MVVM

• Data Binding

• ViewModels don’t know the Views

• ViewModels work as a View Contract

• Abstracting the UI Framework

• Async by design

Page 20: Making Cross-Platform apps with Xamarin

MVVM in action

Page 21: Making Cross-Platform apps with Xamarin

Portable Class Libraries

•Platform Independent

•Subset of target platform

framework

•Reusable

Windows Phone

Android

iOS

Windows

AvailableAPIs

Page 22: Making Cross-Platform apps with Xamarin

Shared Projects

• Does not generate a dll

• Is compiled with the target project

• Good for configuration

• Enables #if directives to make specific code for the target platform (messy)

• Partial classes (clean)

Page 23: Making Cross-Platform apps with Xamarin

Shared Projects in action

Page 24: Making Cross-Platform apps with Xamarin

Challenge #2

Page 25: Making Cross-Platform apps with Xamarin

Challenge #3

Page 26: Making Cross-Platform apps with Xamarin

Challenge #4

Page 27: Making Cross-Platform apps with Xamarin

Challenge #5

Page 28: Making Cross-Platform apps with Xamarin

Dropbox OAuth

Page 29: Making Cross-Platform apps with Xamarin

Challenge #6

Page 30: Making Cross-Platform apps with Xamarin

Challenge #7

Page 31: Making Cross-Platform apps with Xamarin

Lifecycle - Android

Created

Stopped

PausedStarted Resumed

Destroyed

Android

OnCreate

OnStart

OnResume

OnResume

OnPause

OnStop

OnRestart

OnStart

OnDestroy

Page 32: Making Cross-Platform apps with Xamarin

Lifecycle - iOS

Inactive ActiveNot Running

Suspended

Background

Page 33: Making Cross-Platform apps with Xamarin

Lifecycle - Windows

Running

SuspendedNot Running

Activated Suspending

Resuming

Page 34: Making Cross-Platform apps with Xamarin

Challenge #8

Page 35: Making Cross-Platform apps with Xamarin

Challenge #9

Page 36: Making Cross-Platform apps with Xamarin

Localisation - Android

• All resources can be localized

• It is required to at least a default set of resources

• To add localized resources, create a new folder

<directory>-<locale>

Page 37: Making Cross-Platform apps with Xamarin

Localisation - Android

<TextView

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:text="@string/[MY_STRING_RESOURCE]" />

contextInstance.GetString(Resource.String.[MY_STRING_RESOURCE]);

Resources.Configuration.Locale = new Locale("pt-PT");

Resources.UpdateConfiguration(Resources.Configuration, Resources.DisplayMetrics);

Language

Configuration

Getting a

resource

Using a

resource

Page 38: Making Cross-Platform apps with Xamarin

Localisation - iOS

• All resources can be localized

• Can have different configurations

Page 39: Making Cross-Platform apps with Xamarin

Localisation - iOS

NSBundle.MainBundle.LocalizedString (“[MY_RESOURCE_ID]“);

NSUserDefaults.StandardUserDefaults.SetValueForKey(

NSArray.FromStrings("pt"),

new NSString("AppleLanguages"));

NSUserDefaults.StandardUserDefaults.Synchronize();

Language

Configuration

Getting a

resource

Page 40: Making Cross-Platform apps with Xamarin

Localisation - Windows

• String resources can be directly linked to UI

Controls

<TextBlock x:Uid="MyNotes"/>

<TextBlock Text="My Notes" WaterMark="A WaterMark" />

Configures

At Runtime the framework will fill both properties

Page 41: Making Cross-Platform apps with Xamarin

Localisation - Windows

<TextBlock x:Uid=“[My_Resource]"/>

ResourceLoader.GetForCurrentView().GetString("LoadingNotes");

ApplicationLanguages.PrimaryLanguageOverride = "pt“;

Language

Configuration

Getting a

resource

Using a

resource

Page 42: Making Cross-Platform apps with Xamarin

Challenge #10

Page 43: Making Cross-Platform apps with Xamarin

Challenge #11

Page 44: Making Cross-Platform apps with Xamarin

Challenge #12

Page 45: Making Cross-Platform apps with Xamarin

Where to go next

•Nuget

•Xamarin Components

•HttpClient

•Dependency Injection

•MVVM Light & MVVM Cross

•Xamarin Forms

• Interface based programming

•Portable app lifecycle

•Resource centralization

•Extending the MVVM pattern

•Taking control of the UI Thread

•Make apps!

Page 46: Making Cross-Platform apps with Xamarin

THANK YOU TO OUR

SPONSORS