24
9 Settembre 2016 Xamarin Solution Architectures: PCL Vs. Shared Gaetano Paternò [email protected] @tanopaterno

Unofficial Xamarin Day DomusDotNet

Embed Size (px)

Citation preview

Page 1: Unofficial Xamarin Day DomusDotNet

9 Settembre 2016

Xamarin Solution Architectures: PCL Vs. SharedGaetano Paternò[email protected]@tanopaterno

Page 2: Unofficial Xamarin Day DomusDotNet

9 Settembre 2016

Agenda

• Shared project• PCL e DependencyService• Gestire le differenze• I Custom Renderer• .NET Standard Library

Page 3: Unofficial Xamarin Day DomusDotNet

9 Settembre 2016

Shared or Portable

Portable Class Library or Shared Project

C# Platform Specific

C# Platform Specific

C# Platform Specific

Shared C# Interface Code (Xamarin.Forms)

Shared C# App Logic (ViewModels, Models etc)

Page 4: Unofficial Xamarin Day DomusDotNet

9 Settembre 2016

Shared Project

• Un tipo di progetto speciale, che non produce una DLL in fase di compilazione• I file contenuti al suo interno vengono replicati nei singoli progetti,

con il meccanismo dei link di Visual Studio• Stesso meccanismo usato nelle Universal Windows app per

condividere codice tra Windows e Windows Phone

Page 5: Unofficial Xamarin Day DomusDotNet

9 Settembre 2016

Shared ProjectPer gestire le differenze tra le piattaforme si utilizza la compilazione condizionale, come nelle Universal Windows apppublic void DoSomething(){ #if WINDOWS_PHONE //utilizzo le API di Windows Phone #endif

#if __ANDROID__ //utilizzo le API di Android #endif

#if __IOS__ //utilizzo le API di iOS #endif}

Page 6: Unofficial Xamarin Day DomusDotNet

9 Settembre 2016

Shared Project

Nell’ambito Xamarin Forms, sconsiglio questo approccio perché rende il codice poco comprensibile e difficile da leggere, soprattutto in caso di utilizzo di molte API differenti

Page 7: Unofficial Xamarin Day DomusDotNet

9 Settembre 2016

DemoShared Project

Page 8: Unofficial Xamarin Day DomusDotNet

9 Settembre 2016

Portable Class Libraries (PCL)

• Il progetto produce una libreria DLL, compatibile con tecnologie differenti (Windows, Windows Phone, Xamarin, ecc.)• Supporta il set di API minimo condivisibile tra le piattaforme

selezionate• Non è possibile utilizzare la compilazione condizionale

Page 9: Unofficial Xamarin Day DomusDotNet

9 Settembre 2016

DependencyService

• Si crea un’interfaccia nel progetto condiviso, che espone i metodi comuni tra tutte le piattaforme• Si crea un’implementazione dell’interfaccia specifica per ogni

piattaforma, che utilizza le API di Xamarin• Tramite la classe DependencyService, si ottiene un riferimento

all’implementazione specifica per la piattaforma corrente

Page 10: Unofficial Xamarin Day DomusDotNet

9 Settembre 2016

DependencyService

Portable Class Library

INetworkService

NetworkService

NetworkService

NetworkService

[assembly: Dependency(typeof(NetworkService))]

Page 11: Unofficial Xamarin Day DomusDotNet

9 Settembre 2016

Portable Class Library e DependencyService

Page 12: Unofficial Xamarin Day DomusDotNet

9 Settembre 2016

Gestire le differenzeXamarin Forms offre la classe Device, che:• Permette di distinguere la piattaforma e implementare variazioni nel

layout• Permette di effettuare alcune operazioni cross-platform

direttamente nel progetto condiviso:• Invocare un URL -> OpenUri()• Avviare un timer -> StartTimer()• Eseguire un’operazione sul thread della UI ->

BeginInvokeOnMainThread()

Page 13: Unofficial Xamarin Day DomusDotNet

9 Settembre 2016

Le differenze nel layout

<Label> <Label.FontSize> <OnPlatform x:TypeArguments="x:Double" WinPhone="20" iOS="25" Android="30" /> </Label.FontSize></Label>

Page 14: Unofficial Xamarin Day DomusDotNet

9 Settembre 2016

Gestire le differenze

Page 15: Unofficial Xamarin Day DomusDotNet

9 Settembre 2016

CustomRenderer

• E’ il meccanismo di Xamarin Forms che consente di personalizzare l’aspetto e il comportamento dei controlli in base alla piattaforma• Un controllo base, nel progetto condiviso, da utilizzare nelle pagine

XAML• Una classe in ogni progetto specifico, in grado di utilizzare le API

native della piattaforma

Page 16: Unofficial Xamarin Day DomusDotNet

9 Settembre 2016

CustomRenderer

Portable Class Library

CustomLabel

CustomLabelRenderer

CustomLabelRenderer

CustomLabelRenderer

[assembly: ExportRenderer(typeof(CustomLabel), typeof(CustomLabelRenderer))]

Page 17: Unofficial Xamarin Day DomusDotNet

9 Settembre 2016

CustomRenderer

Page 18: Unofficial Xamarin Day DomusDotNet

9 Settembre 2016

.Net Standard Library

Page 19: Unofficial Xamarin Day DomusDotNet

9 Settembre 2016

.Net Standard Library

Page 20: Unofficial Xamarin Day DomusDotNet

9 Settembre 2016

.Net Standard Library

Page 21: Unofficial Xamarin Day DomusDotNet

9 Settembre 2016

.Net Standard Library

Page 22: Unofficial Xamarin Day DomusDotNet

9 Settembre 2016

Creating Mobile Apps with Xamarin.Forms Book

• https://developer.xamarin.com/guides/xamarin-forms/creating-mobile-apps-xamarin-forms/

Page 23: Unofficial Xamarin Day DomusDotNet

9 Settembre 2016

Visual Studio Dev Essentials

• https://www.visualstudio.com/products/visual-studio-dev-essentials-vs

Page 24: Unofficial Xamarin Day DomusDotNet

9 Settembre 2016

Question time

• Shared project• PCL e DependencyService• Gestire le differenze• I Custom Renderer• .NET Standard Library