SlideShare una empresa de Scribd logo
1 de 18
Sviluppare applicazioni cross-
platform con Xamarin Forms e Prism
Matteo Pagani
Windows AppConsult Engineer @ Microsoft
matteo.pagani@microsoft.com
Twitter: @qmatteoq
MILAN 25-26 NOVEMBER 2016
Shared C# codebase • 100% native API access • High performance
Shared C# Business Logic
(Portable Class Library)
Shared C# UI (XML)
XAMARIN FORMS
<?xml version="1.0" encoding="UTF-8"?>
<TabbedPage xmlns=“…" xmlns:x=“…“ x:Class="MyApp.MainPage" > >
<TabbedPage.Children>
<ContentPage Title="Profile" Icon ="Profile.png">
< <StackLayout Spacing="20" Padding="20” VerticalOptions="Center">
<<Entry Placeholder="Username" Text="{Binding Username}">
<<Entry Placeholder="Password” Text="{Binding Password}” IsPassword="true">>
<<Button Text="Login" TextColor="White” BackgroundColor=”…” Command=”…">/>
</<StackLayout>>
</ContentPage>>
<ContentPage Title = "Settings"
Icon = "Settings.png">>
<!-- Settings -->
</ <ContentPage>>
</TabbedPage.Children>
XAMARIN FORMS
THE MVVM PATTERN
PRISM FOR XAMARIN FORMS
• Prism was a MVVM framework originally created by the Patterns &
Practises division by Microsoft
• Now it’s an open source project driven by Brian Lagunas, a Microsoft MVP,
and by the community
• It provides the basic infrastructure to implement the MVVM pattern:
• Base class for ViewModels
• Dependency injection container with multiple framework choices
• Messages
• It provides advanced features to solve specific requirements of Xamarin
Forms:
• Navigation from a ViewModel
• Handling the page’s lifecycle
• Handling platform specific code
• Deep linking
https://github.com/PrismLibrary
CONFIGURATION
• Manually, by adding the following NuGet packages:
• Prism.Core
• Prism.Forms
• Prism.xyz.Forms, where xyz is one of the supported dependency
injection containers (Unity, Ninject, Autofact, etc.)
• Automatically, with the project wizard:
DEPENDENCY INJECTION
With the standard approach, objects that are
used in a ViewModel are created at compile time:
public MainViewModel MainViewModel
{
public MainViewModel()
{
DataService service = new DataService();
service.GetItems();
}
}
DEPENDENCY INJECTION
MainViewModel IDataService
public MainViewModel(IDataService dataService)
{
}
DataServiceTestDataService
DEPENDENCY INJECTION
• In the App class you associate the interface with its concrete
implementation:
Container.RegisterType<IDataService, DataService>();
• In the constructor of the ViewModel, you create a dependency by adding a
parameter to the class’ constructor:
private readonly IDataService _apiService;
public MainPageViewModel(IDataService apiService)
{
_apiService = apiService;
}
• You use the class in any method or property of your ViewModel
CONFIGURATION
• Your App class needs to inherit from PrismApplication
• RegisterTypes() method to register classes inside the dependency
container.
• OnInitialized() method to navigate to the main page of the app using the
NavigationService provided by Prism
KEYWORD: flexibility
• Default naming convention: MainPage -> MainPageViewModel
• You don’t like it? Feel free to override it and manually specify the
connection in the dependency container.
Container.RegisterTypeForNavigation<MainPage>();
Container.RegisterTypeForNavigation<DetailPage,
MyCustomDetailViewModel>();
NAVIGATION
• INavigationAware is an interface that you can implement in your
ViewModels to get access to the following events:
• OnNavigatedTo() when you navigate to the page
• OnNavigatedFrom() when you navigate away from the page
• OnNavigatingTo() when you are navigating to the page
• NavigationService can be used to navigate from one page to another, with
support to parameters:
NavigationParameters param = new NavigationParameters();
param.Add("text", “This is a parameter");
await _navigationService.NavigateAsync("DetailPage", param);
• In the OnNavigatedTo() method of the destination page you can retrieve
the parameter
NAVIGATION
• Deep link: you can create complex navigation paths and retain the stack of
the pages
await
NavigationService.NavigateAsync("MainTabbedPage/NavigationPage
/ShowsListPage/DetailPage?id=279121");
MainTabbedPage Navigation ShowsList Detail
DEMO
Advanced navigation
HANDLING PLATFORM SPECIFIC CODE
• Xamarin Forms allows to share business logic and user interface but, at
some point, you need to write platform specific code
• Xamarin Forms offers a DependencyService class and a Dependency
attribute to handle this scenario
Portable Class
Library
IShareService
IosShareService
DroidShareService
UwpShareService
[assembly: Dependency(typeof(UwpShareService))]
HANDLING PLATFORM SPECIFIC CODE WITH DI
IShareService shareService =
DependencyService.Get<IShareService>();
Container.RegisterInstance<IShareService>(shareService);
• With a traditional dependency injection approach, you have first to get a
reference to the platform specific service and then register it into the DI
container
• Prism automatically register every class tagged with the Dependency
attribute inside the container, so you can just use it in your ViewModels
private readonly IShareService _shareService;
public MainPageViewModel(IShareService shareService)
{
_shareService = shareService;
}
DEMO
Platform specific code with DI
REFERENCES
• Slides: https://doc.co/P2VavL
• Samples: https://github.com/qmatteoq/XamarinForms-Prism
• Prism: https://github.com/PrismLibrary/
• Templates for Prism:
https://marketplace.visualstudio.com/items?itemName=BrianL
agunas.PrismTemplatePack
• Blog posts with more detailed info about Prism:
http://blog.qmatteoq.com/tag/prism/
• Blog posts to learn more about MVVM:
http://blog.qmatteoq.com/the-mvvm-pattern-introduction/
• MVA course about Xamarin Forms: https://aka.ms/xamarin-
forms-mva
Q & A
Matteo Pagani
Windows AppConsult Engineer @ Microsoft
matteo.pagani@microsoft.com
Twitter: @qmatteoq
MILAN 25-26 NOVEMBER 2016

Más contenido relacionado

La actualidad más candente

Client Object Model - SharePoint Extreme 2012
Client Object Model - SharePoint Extreme 2012Client Object Model - SharePoint Extreme 2012
Client Object Model - SharePoint Extreme 2012
daniel plocker
 

La actualidad más candente (20)

Mvc4
Mvc4Mvc4
Mvc4
 
MVC ppt presentation
MVC ppt presentationMVC ppt presentation
MVC ppt presentation
 
ASP.NET MVC 5 - EF 6 - VS2015
ASP.NET MVC 5 - EF 6 - VS2015ASP.NET MVC 5 - EF 6 - VS2015
ASP.NET MVC 5 - EF 6 - VS2015
 
Asp.net MVC training session
Asp.net MVC training sessionAsp.net MVC training session
Asp.net MVC training session
 
Mvc summary
Mvc summaryMvc summary
Mvc summary
 
REPORT ON ASP.NET
REPORT ON ASP.NETREPORT ON ASP.NET
REPORT ON ASP.NET
 
Asp.net mvc presentation by Nitin Sawant
Asp.net mvc presentation by Nitin SawantAsp.net mvc presentation by Nitin Sawant
Asp.net mvc presentation by Nitin Sawant
 
Model View Controller (MVC)
Model View Controller (MVC)Model View Controller (MVC)
Model View Controller (MVC)
 
Basic React Knowledge.
Basic React Knowledge.Basic React Knowledge.
Basic React Knowledge.
 
ASP.NET MVC 3
ASP.NET MVC 3ASP.NET MVC 3
ASP.NET MVC 3
 
ASP .net MVC
ASP .net MVCASP .net MVC
ASP .net MVC
 
Client Object Model - SharePoint Extreme 2012
Client Object Model - SharePoint Extreme 2012Client Object Model - SharePoint Extreme 2012
Client Object Model - SharePoint Extreme 2012
 
Integration with dropbox using mule esb
Integration with dropbox using mule esbIntegration with dropbox using mule esb
Integration with dropbox using mule esb
 
ASP .NET MVC Introduction & Guidelines
ASP .NET MVC Introduction & Guidelines  ASP .NET MVC Introduction & Guidelines
ASP .NET MVC Introduction & Guidelines
 
Spring MVC
Spring MVCSpring MVC
Spring MVC
 
MSDN - ASP.NET MVC
MSDN - ASP.NET MVCMSDN - ASP.NET MVC
MSDN - ASP.NET MVC
 
ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentation
 
MVC
MVCMVC
MVC
 
Xamarin Workshop Noob to Master – Week 5
Xamarin Workshop Noob to Master – Week 5Xamarin Workshop Noob to Master – Week 5
Xamarin Workshop Noob to Master – Week 5
 
Context at design
Context at designContext at design
Context at design
 

Destacado

Destacado (20)

SASI, Cassandra on the full text search ride - DuyHai Doan - Codemotion Milan...
SASI, Cassandra on the full text search ride - DuyHai Doan - Codemotion Milan...SASI, Cassandra on the full text search ride - DuyHai Doan - Codemotion Milan...
SASI, Cassandra on the full text search ride - DuyHai Doan - Codemotion Milan...
 
DevOps in Cloud, dai Container all'approccio Codeless - Gabriele Provinciali,...
DevOps in Cloud, dai Container all'approccio Codeless - Gabriele Provinciali,...DevOps in Cloud, dai Container all'approccio Codeless - Gabriele Provinciali,...
DevOps in Cloud, dai Container all'approccio Codeless - Gabriele Provinciali,...
 
Milano Chatbots Meetup - Vittorio Banfi - Bot Design - Codemotion Milan 2016
Milano Chatbots Meetup - Vittorio Banfi - Bot Design - Codemotion Milan 2016 Milano Chatbots Meetup - Vittorio Banfi - Bot Design - Codemotion Milan 2016
Milano Chatbots Meetup - Vittorio Banfi - Bot Design - Codemotion Milan 2016
 
Put yourself in the appsec pipe - Paolo Perego - Codemotion Milan 2016
Put yourself in the appsec pipe - Paolo Perego - Codemotion Milan 2016Put yourself in the appsec pipe - Paolo Perego - Codemotion Milan 2016
Put yourself in the appsec pipe - Paolo Perego - Codemotion Milan 2016
 
Virtual Reality gaming: analysis of Yon Paradox development - Fabio Mosca - C...
Virtual Reality gaming: analysis of Yon Paradox development - Fabio Mosca - C...Virtual Reality gaming: analysis of Yon Paradox development - Fabio Mosca - C...
Virtual Reality gaming: analysis of Yon Paradox development - Fabio Mosca - C...
 
Keynote: Community Innovation Alaina Percival - Codemotion Milan 2016
Keynote: Community Innovation Alaina Percival - Codemotion Milan 2016Keynote: Community Innovation Alaina Percival - Codemotion Milan 2016
Keynote: Community Innovation Alaina Percival - Codemotion Milan 2016
 
Pronti per la legge sulla data protection GDPR? No Panic! - Stefano Sali, Dom...
Pronti per la legge sulla data protection GDPR? No Panic! - Stefano Sali, Dom...Pronti per la legge sulla data protection GDPR? No Panic! - Stefano Sali, Dom...
Pronti per la legge sulla data protection GDPR? No Panic! - Stefano Sali, Dom...
 
The game design behind Redout - Giuseppe Enrico Franchi - Codemotion Milan 2016
The game design behind Redout - Giuseppe Enrico Franchi - Codemotion Milan 2016The game design behind Redout - Giuseppe Enrico Franchi - Codemotion Milan 2016
The game design behind Redout - Giuseppe Enrico Franchi - Codemotion Milan 2016
 
Keynote: The Most Important Thing - Mike Lee - Codemotion Milan 2016
Keynote: The Most Important Thing - Mike Lee - Codemotion Milan 2016Keynote: The Most Important Thing - Mike Lee - Codemotion Milan 2016
Keynote: The Most Important Thing - Mike Lee - Codemotion Milan 2016
 
Milano Chatbots Meetup - Paolo Montrasio - Codemotion Milan 2016
Milano Chatbots Meetup - Paolo Montrasio - Codemotion Milan 2016Milano Chatbots Meetup - Paolo Montrasio - Codemotion Milan 2016
Milano Chatbots Meetup - Paolo Montrasio - Codemotion Milan 2016
 
Elixir and Lambda talk with a Telegram bot - Paolo Montrasio - Codemotion Mil...
Elixir and Lambda talk with a Telegram bot - Paolo Montrasio - Codemotion Mil...Elixir and Lambda talk with a Telegram bot - Paolo Montrasio - Codemotion Mil...
Elixir and Lambda talk with a Telegram bot - Paolo Montrasio - Codemotion Mil...
 
Games of Simplicity - Pozzi; Molinari - Codemotion Milan 2016
Games of Simplicity - Pozzi; Molinari - Codemotion Milan 2016Games of Simplicity - Pozzi; Molinari - Codemotion Milan 2016
Games of Simplicity - Pozzi; Molinari - Codemotion Milan 2016
 
Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...
Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...
Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...
 
Public speaking 4 geeks - Lorenzo Barbieri - Codemotion Milan 2016
Public speaking 4 geeks - Lorenzo Barbieri - Codemotion Milan 2016Public speaking 4 geeks - Lorenzo Barbieri - Codemotion Milan 2016
Public speaking 4 geeks - Lorenzo Barbieri - Codemotion Milan 2016
 
Big Data, Small Dashboard - Andrea Maietta - Codemotion Milan 2016
Big Data, Small Dashboard - Andrea Maietta - Codemotion Milan 2016Big Data, Small Dashboard - Andrea Maietta - Codemotion Milan 2016
Big Data, Small Dashboard - Andrea Maietta - Codemotion Milan 2016
 
Microservices done right or SOA lessons learnt - Sean Farmar - Codemotion Mil...
Microservices done right or SOA lessons learnt - Sean Farmar - Codemotion Mil...Microservices done right or SOA lessons learnt - Sean Farmar - Codemotion Mil...
Microservices done right or SOA lessons learnt - Sean Farmar - Codemotion Mil...
 
Come rendere il proprio prodotto una bomba creandogli una intera community in...
Come rendere il proprio prodotto una bomba creandogli una intera community in...Come rendere il proprio prodotto una bomba creandogli una intera community in...
Come rendere il proprio prodotto una bomba creandogli una intera community in...
 
Beyond Hacktivism - why #policymatters - Zan Markan - Codemotion Milan 2016
Beyond Hacktivism - why #policymatters - Zan Markan - Codemotion Milan 2016Beyond Hacktivism - why #policymatters - Zan Markan - Codemotion Milan 2016
Beyond Hacktivism - why #policymatters - Zan Markan - Codemotion Milan 2016
 
Understanding Angular 2 - Shmuela Jacobs - Codemotion Milan 2016
Understanding Angular 2 - Shmuela Jacobs - Codemotion Milan 2016Understanding Angular 2 - Shmuela Jacobs - Codemotion Milan 2016
Understanding Angular 2 - Shmuela Jacobs - Codemotion Milan 2016
 
We started with RoR, C++, C#, nodeJS and... at the end we chose GO - Maurizio...
We started with RoR, C++, C#, nodeJS and... at the end we chose GO - Maurizio...We started with RoR, C++, C#, nodeJS and... at the end we chose GO - Maurizio...
We started with RoR, C++, C#, nodeJS and... at the end we chose GO - Maurizio...
 

Similar a Sviluppare applicazioni cross-platform con Xamarin Forms e il framework Prism - Matteo Pagani - Codemotion Milan 2016

Spring first in Magnolia CMS - Spring I/O 2015
Spring first in Magnolia CMS - Spring I/O 2015Spring first in Magnolia CMS - Spring I/O 2015
Spring first in Magnolia CMS - Spring I/O 2015
Tobias Mattsson
 
Rp 6 session 2 naresh bhatia
Rp 6  session 2 naresh bhatiaRp 6  session 2 naresh bhatia
Rp 6 session 2 naresh bhatia
sapientindia
 
Advanced SharePoint 2010 and 2013 Web Part Development by Rob Windsor - SPTec...
Advanced SharePoint 2010 and 2013 Web Part Development by Rob Windsor - SPTec...Advanced SharePoint 2010 and 2013 Web Part Development by Rob Windsor - SPTec...
Advanced SharePoint 2010 and 2013 Web Part Development by Rob Windsor - SPTec...
SPTechCon
 

Similar a Sviluppare applicazioni cross-platform con Xamarin Forms e il framework Prism - Matteo Pagani - Codemotion Milan 2016 (20)

.NET Fest 2017. Matteo Pagani. Prism and Xamarin Forms: create cross-platform...
.NET Fest 2017. Matteo Pagani. Prism and Xamarin Forms: create cross-platform....NET Fest 2017. Matteo Pagani. Prism and Xamarin Forms: create cross-platform...
.NET Fest 2017. Matteo Pagani. Prism and Xamarin Forms: create cross-platform...
 
Spring first in Magnolia CMS - Spring I/O 2015
Spring first in Magnolia CMS - Spring I/O 2015Spring first in Magnolia CMS - Spring I/O 2015
Spring first in Magnolia CMS - Spring I/O 2015
 
Cross platform mobile app development with Xamarin
Cross platform mobile app development with XamarinCross platform mobile app development with Xamarin
Cross platform mobile app development with Xamarin
 
App innovationcircles xamarin
App innovationcircles xamarinApp innovationcircles xamarin
App innovationcircles xamarin
 
Rp 6 session 2 naresh bhatia
Rp 6  session 2 naresh bhatiaRp 6  session 2 naresh bhatia
Rp 6 session 2 naresh bhatia
 
Getting Healthy with Magnolia, Blossom and Spring
Getting Healthy with Magnolia, Blossom and SpringGetting Healthy with Magnolia, Blossom and Spring
Getting Healthy with Magnolia, Blossom and Spring
 
A Walking Tour of (almost) all of Springdom
A Walking Tour of (almost) all of Springdom A Walking Tour of (almost) all of Springdom
A Walking Tour of (almost) all of Springdom
 
Angular server side rendering - Strategies & Technics
Angular server side rendering - Strategies & Technics Angular server side rendering - Strategies & Technics
Angular server side rendering - Strategies & Technics
 
Summit Australia 2019 - PowerApp Portals - Andrew Ly & Lachlan Wright
Summit Australia 2019 - PowerApp Portals - Andrew Ly & Lachlan WrightSummit Australia 2019 - PowerApp Portals - Andrew Ly & Lachlan Wright
Summit Australia 2019 - PowerApp Portals - Andrew Ly & Lachlan Wright
 
Advanced SharePoint 2010 and 2013 Web Part Development by Rob Windsor - SPTec...
Advanced SharePoint 2010 and 2013 Web Part Development by Rob Windsor - SPTec...Advanced SharePoint 2010 and 2013 Web Part Development by Rob Windsor - SPTec...
Advanced SharePoint 2010 and 2013 Web Part Development by Rob Windsor - SPTec...
 
Implementation of azure active directory authentication with cross platform d...
Implementation of azure active directory authentication with cross platform d...Implementation of azure active directory authentication with cross platform d...
Implementation of azure active directory authentication with cross platform d...
 
SharePoint Saturday Belgium 2018 - APIs, APIs everywhere!
SharePoint Saturday Belgium 2018 - APIs, APIs everywhere!SharePoint Saturday Belgium 2018 - APIs, APIs everywhere!
SharePoint Saturday Belgium 2018 - APIs, APIs everywhere!
 
APIs, APIs Everywhere!
APIs, APIs Everywhere!APIs, APIs Everywhere!
APIs, APIs Everywhere!
 
SharePoint Saturday Atlanta 2015
SharePoint Saturday Atlanta 2015SharePoint Saturday Atlanta 2015
SharePoint Saturday Atlanta 2015
 
Real time dashboards with Kafka and Druid
Real time dashboards with Kafka and DruidReal time dashboards with Kafka and Druid
Real time dashboards with Kafka and Druid
 
Introduction to Xamarin.Forms 2.x
Introduction to Xamarin.Forms 2.xIntroduction to Xamarin.Forms 2.x
Introduction to Xamarin.Forms 2.x
 
Multi Client Development with Spring
Multi Client Development with SpringMulti Client Development with Spring
Multi Client Development with Spring
 
Spring and Web Content Management
Spring and Web Content ManagementSpring and Web Content Management
Spring and Web Content Management
 
Xamarin microsoft graph
Xamarin microsoft graphXamarin microsoft graph
Xamarin microsoft graph
 
What's new in Xamarin.Forms?
What's new in Xamarin.Forms?What's new in Xamarin.Forms?
What's new in Xamarin.Forms?
 

Más de Codemotion

Más de Codemotion (20)

Fuzz-testing: A hacker's approach to making your code more secure | Pascal Ze...
Fuzz-testing: A hacker's approach to making your code more secure | Pascal Ze...Fuzz-testing: A hacker's approach to making your code more secure | Pascal Ze...
Fuzz-testing: A hacker's approach to making your code more secure | Pascal Ze...
 
Pompili - From hero to_zero: The FatalNoise neverending story
Pompili - From hero to_zero: The FatalNoise neverending storyPompili - From hero to_zero: The FatalNoise neverending story
Pompili - From hero to_zero: The FatalNoise neverending story
 
Pastore - Commodore 65 - La storia
Pastore - Commodore 65 - La storiaPastore - Commodore 65 - La storia
Pastore - Commodore 65 - La storia
 
Pennisi - Essere Richard Altwasser
Pennisi - Essere Richard AltwasserPennisi - Essere Richard Altwasser
Pennisi - Essere Richard Altwasser
 
Michel Schudel - Let's build a blockchain... in 40 minutes! - Codemotion Amst...
Michel Schudel - Let's build a blockchain... in 40 minutes! - Codemotion Amst...Michel Schudel - Let's build a blockchain... in 40 minutes! - Codemotion Amst...
Michel Schudel - Let's build a blockchain... in 40 minutes! - Codemotion Amst...
 
Richard Süselbeck - Building your own ride share app - Codemotion Amsterdam 2019
Richard Süselbeck - Building your own ride share app - Codemotion Amsterdam 2019Richard Süselbeck - Building your own ride share app - Codemotion Amsterdam 2019
Richard Süselbeck - Building your own ride share app - Codemotion Amsterdam 2019
 
Eward Driehuis - What we learned from 20.000 attacks - Codemotion Amsterdam 2019
Eward Driehuis - What we learned from 20.000 attacks - Codemotion Amsterdam 2019Eward Driehuis - What we learned from 20.000 attacks - Codemotion Amsterdam 2019
Eward Driehuis - What we learned from 20.000 attacks - Codemotion Amsterdam 2019
 
Francesco Baldassarri - Deliver Data at Scale - Codemotion Amsterdam 2019 -
Francesco Baldassarri  - Deliver Data at Scale - Codemotion Amsterdam 2019 - Francesco Baldassarri  - Deliver Data at Scale - Codemotion Amsterdam 2019 -
Francesco Baldassarri - Deliver Data at Scale - Codemotion Amsterdam 2019 -
 
Martin Förtsch, Thomas Endres - Stereoscopic Style Transfer AI - Codemotion A...
Martin Förtsch, Thomas Endres - Stereoscopic Style Transfer AI - Codemotion A...Martin Förtsch, Thomas Endres - Stereoscopic Style Transfer AI - Codemotion A...
Martin Förtsch, Thomas Endres - Stereoscopic Style Transfer AI - Codemotion A...
 
Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...
Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...
Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...
 
Angelo van der Sijpt - How well do you know your network stack? - Codemotion ...
Angelo van der Sijpt - How well do you know your network stack? - Codemotion ...Angelo van der Sijpt - How well do you know your network stack? - Codemotion ...
Angelo van der Sijpt - How well do you know your network stack? - Codemotion ...
 
Lars Wolff - Performance Testing for DevOps in the Cloud - Codemotion Amsterd...
Lars Wolff - Performance Testing for DevOps in the Cloud - Codemotion Amsterd...Lars Wolff - Performance Testing for DevOps in the Cloud - Codemotion Amsterd...
Lars Wolff - Performance Testing for DevOps in the Cloud - Codemotion Amsterd...
 
Sascha Wolter - Conversational AI Demystified - Codemotion Amsterdam 2019
Sascha Wolter - Conversational AI Demystified - Codemotion Amsterdam 2019Sascha Wolter - Conversational AI Demystified - Codemotion Amsterdam 2019
Sascha Wolter - Conversational AI Demystified - Codemotion Amsterdam 2019
 
Michele Tonutti - Scaling is caring - Codemotion Amsterdam 2019
Michele Tonutti - Scaling is caring - Codemotion Amsterdam 2019Michele Tonutti - Scaling is caring - Codemotion Amsterdam 2019
Michele Tonutti - Scaling is caring - Codemotion Amsterdam 2019
 
Pat Hermens - From 100 to 1,000+ deployments a day - Codemotion Amsterdam 2019
Pat Hermens - From 100 to 1,000+ deployments a day - Codemotion Amsterdam 2019Pat Hermens - From 100 to 1,000+ deployments a day - Codemotion Amsterdam 2019
Pat Hermens - From 100 to 1,000+ deployments a day - Codemotion Amsterdam 2019
 
James Birnie - Using Many Worlds of Compute Power with Quantum - Codemotion A...
James Birnie - Using Many Worlds of Compute Power with Quantum - Codemotion A...James Birnie - Using Many Worlds of Compute Power with Quantum - Codemotion A...
James Birnie - Using Many Worlds of Compute Power with Quantum - Codemotion A...
 
Don Goodman-Wilson - Chinese food, motor scooters, and open source developmen...
Don Goodman-Wilson - Chinese food, motor scooters, and open source developmen...Don Goodman-Wilson - Chinese food, motor scooters, and open source developmen...
Don Goodman-Wilson - Chinese food, motor scooters, and open source developmen...
 
Pieter Omvlee - The story behind Sketch - Codemotion Amsterdam 2019
Pieter Omvlee - The story behind Sketch - Codemotion Amsterdam 2019Pieter Omvlee - The story behind Sketch - Codemotion Amsterdam 2019
Pieter Omvlee - The story behind Sketch - Codemotion Amsterdam 2019
 
Dave Farley - Taking Back “Software Engineering” - Codemotion Amsterdam 2019
Dave Farley - Taking Back “Software Engineering” - Codemotion Amsterdam 2019Dave Farley - Taking Back “Software Engineering” - Codemotion Amsterdam 2019
Dave Farley - Taking Back “Software Engineering” - Codemotion Amsterdam 2019
 
Joshua Hoffman - Should the CTO be Coding? - Codemotion Amsterdam 2019
Joshua Hoffman - Should the CTO be Coding? - Codemotion Amsterdam 2019Joshua Hoffman - Should the CTO be Coding? - Codemotion Amsterdam 2019
Joshua Hoffman - Should the CTO be Coding? - Codemotion Amsterdam 2019
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

Sviluppare applicazioni cross-platform con Xamarin Forms e il framework Prism - Matteo Pagani - Codemotion Milan 2016

  • 1. Sviluppare applicazioni cross- platform con Xamarin Forms e Prism Matteo Pagani Windows AppConsult Engineer @ Microsoft matteo.pagani@microsoft.com Twitter: @qmatteoq MILAN 25-26 NOVEMBER 2016
  • 2. Shared C# codebase • 100% native API access • High performance Shared C# Business Logic (Portable Class Library) Shared C# UI (XML) XAMARIN FORMS
  • 3. <?xml version="1.0" encoding="UTF-8"?> <TabbedPage xmlns=“…" xmlns:x=“…“ x:Class="MyApp.MainPage" > > <TabbedPage.Children> <ContentPage Title="Profile" Icon ="Profile.png"> < <StackLayout Spacing="20" Padding="20” VerticalOptions="Center"> <<Entry Placeholder="Username" Text="{Binding Username}"> <<Entry Placeholder="Password” Text="{Binding Password}” IsPassword="true">> <<Button Text="Login" TextColor="White” BackgroundColor=”…” Command=”…">/> </<StackLayout>> </ContentPage>> <ContentPage Title = "Settings" Icon = "Settings.png">> <!-- Settings --> </ <ContentPage>> </TabbedPage.Children> XAMARIN FORMS
  • 5. PRISM FOR XAMARIN FORMS • Prism was a MVVM framework originally created by the Patterns & Practises division by Microsoft • Now it’s an open source project driven by Brian Lagunas, a Microsoft MVP, and by the community • It provides the basic infrastructure to implement the MVVM pattern: • Base class for ViewModels • Dependency injection container with multiple framework choices • Messages • It provides advanced features to solve specific requirements of Xamarin Forms: • Navigation from a ViewModel • Handling the page’s lifecycle • Handling platform specific code • Deep linking https://github.com/PrismLibrary
  • 6. CONFIGURATION • Manually, by adding the following NuGet packages: • Prism.Core • Prism.Forms • Prism.xyz.Forms, where xyz is one of the supported dependency injection containers (Unity, Ninject, Autofact, etc.) • Automatically, with the project wizard:
  • 7. DEPENDENCY INJECTION With the standard approach, objects that are used in a ViewModel are created at compile time: public MainViewModel MainViewModel { public MainViewModel() { DataService service = new DataService(); service.GetItems(); } }
  • 8. DEPENDENCY INJECTION MainViewModel IDataService public MainViewModel(IDataService dataService) { } DataServiceTestDataService
  • 9. DEPENDENCY INJECTION • In the App class you associate the interface with its concrete implementation: Container.RegisterType<IDataService, DataService>(); • In the constructor of the ViewModel, you create a dependency by adding a parameter to the class’ constructor: private readonly IDataService _apiService; public MainPageViewModel(IDataService apiService) { _apiService = apiService; } • You use the class in any method or property of your ViewModel
  • 10. CONFIGURATION • Your App class needs to inherit from PrismApplication • RegisterTypes() method to register classes inside the dependency container. • OnInitialized() method to navigate to the main page of the app using the NavigationService provided by Prism KEYWORD: flexibility • Default naming convention: MainPage -> MainPageViewModel • You don’t like it? Feel free to override it and manually specify the connection in the dependency container. Container.RegisterTypeForNavigation<MainPage>(); Container.RegisterTypeForNavigation<DetailPage, MyCustomDetailViewModel>();
  • 11. NAVIGATION • INavigationAware is an interface that you can implement in your ViewModels to get access to the following events: • OnNavigatedTo() when you navigate to the page • OnNavigatedFrom() when you navigate away from the page • OnNavigatingTo() when you are navigating to the page • NavigationService can be used to navigate from one page to another, with support to parameters: NavigationParameters param = new NavigationParameters(); param.Add("text", “This is a parameter"); await _navigationService.NavigateAsync("DetailPage", param); • In the OnNavigatedTo() method of the destination page you can retrieve the parameter
  • 12. NAVIGATION • Deep link: you can create complex navigation paths and retain the stack of the pages await NavigationService.NavigateAsync("MainTabbedPage/NavigationPage /ShowsListPage/DetailPage?id=279121"); MainTabbedPage Navigation ShowsList Detail
  • 14. HANDLING PLATFORM SPECIFIC CODE • Xamarin Forms allows to share business logic and user interface but, at some point, you need to write platform specific code • Xamarin Forms offers a DependencyService class and a Dependency attribute to handle this scenario Portable Class Library IShareService IosShareService DroidShareService UwpShareService [assembly: Dependency(typeof(UwpShareService))]
  • 15. HANDLING PLATFORM SPECIFIC CODE WITH DI IShareService shareService = DependencyService.Get<IShareService>(); Container.RegisterInstance<IShareService>(shareService); • With a traditional dependency injection approach, you have first to get a reference to the platform specific service and then register it into the DI container • Prism automatically register every class tagged with the Dependency attribute inside the container, so you can just use it in your ViewModels private readonly IShareService _shareService; public MainPageViewModel(IShareService shareService) { _shareService = shareService; }
  • 17. REFERENCES • Slides: https://doc.co/P2VavL • Samples: https://github.com/qmatteoq/XamarinForms-Prism • Prism: https://github.com/PrismLibrary/ • Templates for Prism: https://marketplace.visualstudio.com/items?itemName=BrianL agunas.PrismTemplatePack • Blog posts with more detailed info about Prism: http://blog.qmatteoq.com/tag/prism/ • Blog posts to learn more about MVVM: http://blog.qmatteoq.com/the-mvvm-pattern-introduction/ • MVA course about Xamarin Forms: https://aka.ms/xamarin- forms-mva
  • 18. Q & A Matteo Pagani Windows AppConsult Engineer @ Microsoft matteo.pagani@microsoft.com Twitter: @qmatteoq MILAN 25-26 NOVEMBER 2016