SlideShare a Scribd company logo
1 of 20
Download to read offline
Ryan Paul
Developer Evangelist
Xamarin
Mail: ryan@xamarin.com
Twitter: @segphault
Building Hybrid Apps
with Xamarin
NATIVE
VERSUS
HTML5?
It’s not a battle to the death.
Reframing the Debate
It’s about using the right tool for the right job.
Advantages of HTML
Express high-level design concepts with declarative markup
Tons of control over presentation: gradients, shadows,
transparency, animation, rich text
Vendor-neutral open standards enable widespread support and
broad portability
Easy to learn and use—can tap into existing expertise
Ease of rapid prototyping lets you get great stuff working quickly
•
•
•
•
•
Disadvantages of HTML
Lack of consistency between rendering engines
Difficult to get optimal performance and acceptably responsive user
interface
Lack of native controls means that you can’t match the platform’s
native look and feel
No access to platform-specific functionality
HTML rendering engines confine you to a lowest-common-
denominator user experience
•
•
•
•
•
Give them the high-quality native app that they deserve!
Your users expect better.
This is kind of what it’s like for users when a
mobile developer ships a non-native app:
Advantages of Native Development
Access the full range of rich device capabilities and
native platform features
Native controls supplied by the platform are tailored
for ideal performance and benefit from platform-level
optimizations and hardware acceleration
Native code gives you a lot more room for optimization
and parallelization
Perfectly conform with the standard user interface
conventions of the underlying platform
•
•
•
•
Hybrid Advantages
Decide which parts of your app
should be HTML or native
Expose device capabilities and native
platform features to your HTML
content
Easy to transition to fully native
application if you reach the limits of
HTML
•
•
•
Share code across platforms: C#, JavaScript, and HTML.
Use platform-specific APIs and native user interface controls.
Xamarin for Hybrids
Displaying HTML Content in Native Apps
An HTML view can be a highly effective tool for displaying
rich content in native applications
Ideal for use in scenarios where you need complex formatting
and want to intersperse graphics
With responsive design techniques, content can be made to
seamlessly adapt to different screen sizes and orientations
Razor templating engine can be used in native applications to
generate HTML content that incorporates relevant data
•
•
•
•
A simple Pokédex application built with Xamarin.iOS
DEMO: Razordex
Implementing Razordex
public class Pokemon
{
public int number { get; set; }
public string name { get; set; }
public string primary_type { get; set; }
public string secondary_type { get; set; }
public string biography { get; set; }
public string primary_ability { get; set; }
public string secondary_ability { get; set; }
}
...
var template = new PokemonHTMLView () { Model = detailItem };
webDetailView.LoadHtmlString (
template.GenerateString (),
NSBundle.MainBundle.BundleUrl);
@model Pokemon
<html>
<head>
<link type="text/css" rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<div class="header">Type</div>
<table class="data">
<tr>
<td><b>Primary:</b></td>
<td>@Model.primary_type</td>
</tr>
<tr>
<td><b>Secondary:</b></td>
<td>@Model.secondary_type</td>
</tr>
</table>
</div>
...
No ferrets were harmed in the making
of this app.
There are several ways to enable interaction between the native part
of your application and the HTML view.
Hybrid Communication Techniques
Expose a C# function to JavaScript
<div onclick="Foo.bar('This is a test!')" class="button">Test</div>
class Foo : Java.Lang.Object
{
...
[Export ("bar")]
public void Bar (Java.Lang.String message)
{
Toast.MakeText (context,
"Message called from JavaScript: " + message, ToastLength.Short).Show ();
}
}
...
WebView web = FindViewById<WebView> (Resource.Id.myWeb);
web.Settings.JavaScriptEnabled = true;
web.AddJavascriptInterface (new Foo (this), "Foo");
web.LoadUrl("file:///android_asset/main.html");
Intercept a Link Handler
<a href="message:This is a test!">Test</a>
private class CustomWebViewClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading (WebView view, string url)
{
if (url.StartsWith ("message:")) {
var message = Uri.UnescapeDataString(url.Split(new char[] { ':' }, 2)[1]);
Toast.MakeText (view.Context,
"Message called from JavaScript: " + message, ToastLength.Short).Show ();
return true;
} else return false;
}
}
...
WebView web = FindViewById<WebView> (Resource.Id.myWeb);
web.SetWebViewClient (new CustomWebViewClient ());
web.Settings.JavaScriptEnabled = true;
web.LoadUrl("file:///android_asset/main.html");
Call JavaScript from C#
WebView web = FindViewById<WebView> (Resource.Id.myWeb);
web.Settings.JavaScriptEnabled = true;
web.LoadUrl("file:///android_asset/main.html");
Button button = FindViewById<Button> (Resource.Id.button1);
button.Click += (object sender, EventArgs e) => {
web.LoadUrl("javascript:document.getElementById('button').innerHTML = 'Hello!'");
};
Q&A
THANK YOU

More Related Content

What's hot

Xamarin 3 hieu 19-06
Xamarin 3   hieu 19-06Xamarin 3   hieu 19-06
Xamarin 3 hieu 19-06
Nguyen Hieu
 

What's hot (20)

Cross Platform Mobile Development with C# and Xamarin
Cross Platform Mobile Development with C# and XamarinCross Platform Mobile Development with C# and Xamarin
Cross Platform Mobile Development with C# and Xamarin
 
Hitchhicker's Guide to Using Xamarin Forms with RESTful Services
Hitchhicker's Guide to Using Xamarin Forms with RESTful ServicesHitchhicker's Guide to Using Xamarin Forms with RESTful Services
Hitchhicker's Guide to Using Xamarin Forms with RESTful Services
 
Xamarin
XamarinXamarin
Xamarin
 
Smaller Not Taller: Defeating the mobile application architecture giant
Smaller Not Taller: Defeating the mobile application architecture giantSmaller Not Taller: Defeating the mobile application architecture giant
Smaller Not Taller: Defeating the mobile application architecture giant
 
Cross Platform Development with Xamarin
Cross Platform Development with XamarinCross Platform Development with Xamarin
Cross Platform Development with Xamarin
 
Native i os, android, and windows development in c# with xamarin 4
Native i os, android, and windows development in c# with xamarin 4Native i os, android, and windows development in c# with xamarin 4
Native i os, android, and windows development in c# with xamarin 4
 
Highlights from the Xamarin Evolve 2016 conference
Highlights from the Xamarin Evolve 2016 conferenceHighlights from the Xamarin Evolve 2016 conference
Highlights from the Xamarin Evolve 2016 conference
 
Introduction to xamarin
Introduction to xamarinIntroduction to xamarin
Introduction to xamarin
 
Cross Platform Mobile Development
Cross Platform Mobile DevelopmentCross Platform Mobile Development
Cross Platform Mobile Development
 
Xamarin 3 hieu 19-06
Xamarin 3   hieu 19-06Xamarin 3   hieu 19-06
Xamarin 3 hieu 19-06
 
Introduction to xamarin
Introduction to xamarinIntroduction to xamarin
Introduction to xamarin
 
Xamarin University Presents: Building Your First Intelligent App with Xamarin...
Xamarin University Presents: Building Your First Intelligent App with Xamarin...Xamarin University Presents: Building Your First Intelligent App with Xamarin...
Xamarin University Presents: Building Your First Intelligent App with Xamarin...
 
Xamarin Cross-Platform with Xamarin.Form, MvvmCross
Xamarin Cross-Platform with Xamarin.Form, MvvmCrossXamarin Cross-Platform with Xamarin.Form, MvvmCross
Xamarin Cross-Platform with Xamarin.Form, MvvmCross
 
Developing and Designing Native Mobile Apps in Xamarin Studio
Developing and Designing Native Mobile Apps in Xamarin StudioDeveloping and Designing Native Mobile Apps in Xamarin Studio
Developing and Designing Native Mobile Apps in Xamarin Studio
 
Introduction to Xamarin
Introduction to XamarinIntroduction to Xamarin
Introduction to Xamarin
 
Optimizing and Extending Xamarin.Forms iOS, Android, and UWP Apps
Optimizing and Extending Xamarin.Forms iOS, Android, and UWP AppsOptimizing and Extending Xamarin.Forms iOS, Android, and UWP Apps
Optimizing and Extending Xamarin.Forms iOS, Android, and UWP Apps
 
Cross Platform Mobile Development with Xamarin
Cross Platform Mobile Development with XamarinCross Platform Mobile Development with Xamarin
Cross Platform Mobile Development with Xamarin
 
Intro to Xamarin for Visual Studio: Native iOS, Android, and Windows Apps in C#
Intro to Xamarin for Visual Studio: Native iOS, Android, and Windows Apps in C#Intro to Xamarin for Visual Studio: Native iOS, Android, and Windows Apps in C#
Intro to Xamarin for Visual Studio: Native iOS, Android, and Windows Apps in C#
 
Cross platform mobile development with xamarin and office 365
Cross platform mobile development with xamarin and office 365Cross platform mobile development with xamarin and office 365
Cross platform mobile development with xamarin and office 365
 
Xamarin介紹
Xamarin介紹Xamarin介紹
Xamarin介紹
 

Similar to Building hybrid apps with Xamarin, Ryan Paul

2018 p.ranjan.raja msc-13_year_ex_php_webdeveloper
2018 p.ranjan.raja msc-13_year_ex_php_webdeveloper2018 p.ranjan.raja msc-13_year_ex_php_webdeveloper
2018 p.ranjan.raja msc-13_year_ex_php_webdeveloper
php2ranjan
 
Mobile Tech Strategies - 4 Sept 2016 V6
Mobile Tech Strategies - 4 Sept 2016 V6Mobile Tech Strategies - 4 Sept 2016 V6
Mobile Tech Strategies - 4 Sept 2016 V6
Ian Morrison
 
ucla_curriculum_overview_12116
ucla_curriculum_overview_12116ucla_curriculum_overview_12116
ucla_curriculum_overview_12116
John Nguyen
 
HTML5: Next Generation Web Development
HTML5: Next Generation Web DevelopmentHTML5: Next Generation Web Development
HTML5: Next Generation Web Development
Dipesh Mukerji
 

Similar to Building hybrid apps with Xamarin, Ryan Paul (20)

App innovationcircles xamarin
App innovationcircles xamarinApp innovationcircles xamarin
App innovationcircles xamarin
 
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
 
SharePoint Mobile App Development with Xmarin
SharePoint Mobile App Development with XmarinSharePoint Mobile App Development with Xmarin
SharePoint Mobile App Development with Xmarin
 
tomtaila
tomtailatomtaila
tomtaila
 
2018 p.ranjan.raja msc-13_year_ex_php_webdeveloper
2018 p.ranjan.raja msc-13_year_ex_php_webdeveloper2018 p.ranjan.raja msc-13_year_ex_php_webdeveloper
2018 p.ranjan.raja msc-13_year_ex_php_webdeveloper
 
DOT NET FULL STACK.pptx
DOT NET FULL STACK.pptxDOT NET FULL STACK.pptx
DOT NET FULL STACK.pptx
 
Sharif
SharifSharif
Sharif
 
Sharif
SharifSharif
Sharif
 
Introduction to xamarin
Introduction to xamarinIntroduction to xamarin
Introduction to xamarin
 
rohit sharma
rohit sharmarohit sharma
rohit sharma
 
Intro to Salesforce Lightning Web Components (LWC)
Intro to Salesforce Lightning Web Components (LWC)Intro to Salesforce Lightning Web Components (LWC)
Intro to Salesforce Lightning Web Components (LWC)
 
Mobile Tech Strategies - 4 Sept 2016 V6
Mobile Tech Strategies - 4 Sept 2016 V6Mobile Tech Strategies - 4 Sept 2016 V6
Mobile Tech Strategies - 4 Sept 2016 V6
 
ucla_curriculum_overview_12116
ucla_curriculum_overview_12116ucla_curriculum_overview_12116
ucla_curriculum_overview_12116
 
Resume ram-krishna
Resume ram-krishnaResume ram-krishna
Resume ram-krishna
 
Writing Code to Work Against any Salesforce Object
Writing Code to Work Against any Salesforce ObjectWriting Code to Work Against any Salesforce Object
Writing Code to Work Against any Salesforce Object
 
How to build and deploy app on Replit
How to build and deploy app on ReplitHow to build and deploy app on Replit
How to build and deploy app on Replit
 
How to Hire a .NET developer Step-By-Step Guide.pdf
How to Hire a .NET developer Step-By-Step Guide.pdfHow to Hire a .NET developer Step-By-Step Guide.pdf
How to Hire a .NET developer Step-By-Step Guide.pdf
 
resume
resumeresume
resume
 
Mobile web or native app
Mobile web or native appMobile web or native app
Mobile web or native app
 
HTML5: Next Generation Web Development
HTML5: Next Generation Web DevelopmentHTML5: Next Generation Web Development
HTML5: Next Generation Web Development
 

More from Xamarin

More from Xamarin (20)

Xamarin University Presents: Ship Better Apps with Visual Studio App Center
Xamarin University Presents: Ship Better Apps with Visual Studio App CenterXamarin University Presents: Ship Better Apps with Visual Studio App Center
Xamarin University Presents: Ship Better Apps with Visual Studio App Center
 
Get the Most Out of iOS 11 with Visual Studio Tools for Xamarin
Get the Most Out of iOS 11 with Visual Studio Tools for XamarinGet the Most Out of iOS 11 with Visual Studio Tools for Xamarin
Get the Most Out of iOS 11 with Visual Studio Tools for Xamarin
 
Get the Most out of Android 8 Oreo with Visual Studio Tools for Xamarin
Get the Most out of Android 8 Oreo with Visual Studio Tools for XamarinGet the Most out of Android 8 Oreo with Visual Studio Tools for Xamarin
Get the Most out of Android 8 Oreo with Visual Studio Tools for Xamarin
 
Creative Hacking: Delivering React Native App A/B Testing Using CodePush
Creative Hacking: Delivering React Native App A/B Testing Using CodePushCreative Hacking: Delivering React Native App A/B Testing Using CodePush
Creative Hacking: Delivering React Native App A/B Testing Using CodePush
 
Build Better Games with Unity and Microsoft Azure
Build Better Games with Unity and Microsoft AzureBuild Better Games with Unity and Microsoft Azure
Build Better Games with Unity and Microsoft Azure
 
Exploring UrhoSharp 3D with Xamarin Workbooks
Exploring UrhoSharp 3D with Xamarin WorkbooksExploring UrhoSharp 3D with Xamarin Workbooks
Exploring UrhoSharp 3D with Xamarin Workbooks
 
Desktop Developer’s Guide to Mobile with Visual Studio Tools for Xamarin
Desktop Developer’s Guide to Mobile with Visual Studio Tools for XamarinDesktop Developer’s Guide to Mobile with Visual Studio Tools for Xamarin
Desktop Developer’s Guide to Mobile with Visual Studio Tools for Xamarin
 
Developer’s Intro to Azure Machine Learning
Developer’s Intro to Azure Machine LearningDeveloper’s Intro to Azure Machine Learning
Developer’s Intro to Azure Machine Learning
 
Customizing Xamarin.Forms UI
Customizing Xamarin.Forms UICustomizing Xamarin.Forms UI
Customizing Xamarin.Forms UI
 
Session 4 - Xamarin Partner Program, Events and Resources
Session 4 - Xamarin Partner Program, Events and ResourcesSession 4 - Xamarin Partner Program, Events and Resources
Session 4 - Xamarin Partner Program, Events and Resources
 
Session 3 - Driving Mobile Growth and Profitability
Session 3 - Driving Mobile Growth and ProfitabilitySession 3 - Driving Mobile Growth and Profitability
Session 3 - Driving Mobile Growth and Profitability
 
Session 2 - Emerging Technologies in your Mobile Practice
Session 2 - Emerging Technologies in your Mobile PracticeSession 2 - Emerging Technologies in your Mobile Practice
Session 2 - Emerging Technologies in your Mobile Practice
 
Session 1 - Transformative Opportunities in Mobile and Cloud
Session 1 - Transformative Opportunities in Mobile and Cloud Session 1 - Transformative Opportunities in Mobile and Cloud
Session 1 - Transformative Opportunities in Mobile and Cloud
 
SkiaSharp Graphics for Xamarin.Forms
SkiaSharp Graphics for Xamarin.FormsSkiaSharp Graphics for Xamarin.Forms
SkiaSharp Graphics for Xamarin.Forms
 
Building Games for iOS, macOS, and tvOS with Visual Studio and Azure
Building Games for iOS, macOS, and tvOS with Visual Studio and AzureBuilding Games for iOS, macOS, and tvOS with Visual Studio and Azure
Building Games for iOS, macOS, and tvOS with Visual Studio and Azure
 
Intro to Xamarin.Forms for Visual Studio 2017
Intro to Xamarin.Forms for Visual Studio 2017Intro to Xamarin.Forms for Visual Studio 2017
Intro to Xamarin.Forms for Visual Studio 2017
 
Connected Mobile Apps with Microsoft Azure
Connected Mobile Apps with Microsoft AzureConnected Mobile Apps with Microsoft Azure
Connected Mobile Apps with Microsoft Azure
 
Introduction to Xamarin for Visual Studio 2017
Introduction to Xamarin for Visual Studio 2017Introduction to Xamarin for Visual Studio 2017
Introduction to Xamarin for Visual Studio 2017
 
Building Your First iOS App with Xamarin for Visual Studio
Building Your First iOS App with Xamarin for Visual StudioBuilding Your First iOS App with Xamarin for Visual Studio
Building Your First iOS App with Xamarin for Visual Studio
 
Building Your First Android App with Xamarin
Building Your First Android App with XamarinBuilding Your First Android App with Xamarin
Building Your First Android App with Xamarin
 

Recently uploaded

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
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Quantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingQuantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation Computing
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
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
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
 
Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern Enterprise
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 

Building hybrid apps with Xamarin, Ryan Paul

  • 1. Ryan Paul Developer Evangelist Xamarin Mail: ryan@xamarin.com Twitter: @segphault Building Hybrid Apps with Xamarin
  • 3. It’s not a battle to the death. Reframing the Debate It’s about using the right tool for the right job.
  • 4. Advantages of HTML Express high-level design concepts with declarative markup Tons of control over presentation: gradients, shadows, transparency, animation, rich text Vendor-neutral open standards enable widespread support and broad portability Easy to learn and use—can tap into existing expertise Ease of rapid prototyping lets you get great stuff working quickly • • • • •
  • 5. Disadvantages of HTML Lack of consistency between rendering engines Difficult to get optimal performance and acceptably responsive user interface Lack of native controls means that you can’t match the platform’s native look and feel No access to platform-specific functionality HTML rendering engines confine you to a lowest-common- denominator user experience • • • • •
  • 6.
  • 7. Give them the high-quality native app that they deserve! Your users expect better. This is kind of what it’s like for users when a mobile developer ships a non-native app:
  • 8. Advantages of Native Development Access the full range of rich device capabilities and native platform features Native controls supplied by the platform are tailored for ideal performance and benefit from platform-level optimizations and hardware acceleration Native code gives you a lot more room for optimization and parallelization Perfectly conform with the standard user interface conventions of the underlying platform • • • •
  • 9. Hybrid Advantages Decide which parts of your app should be HTML or native Expose device capabilities and native platform features to your HTML content Easy to transition to fully native application if you reach the limits of HTML • • •
  • 10. Share code across platforms: C#, JavaScript, and HTML. Use platform-specific APIs and native user interface controls. Xamarin for Hybrids
  • 11. Displaying HTML Content in Native Apps An HTML view can be a highly effective tool for displaying rich content in native applications Ideal for use in scenarios where you need complex formatting and want to intersperse graphics With responsive design techniques, content can be made to seamlessly adapt to different screen sizes and orientations Razor templating engine can be used in native applications to generate HTML content that incorporates relevant data • • • •
  • 12. A simple Pokédex application built with Xamarin.iOS DEMO: Razordex
  • 13. Implementing Razordex public class Pokemon { public int number { get; set; } public string name { get; set; } public string primary_type { get; set; } public string secondary_type { get; set; } public string biography { get; set; } public string primary_ability { get; set; } public string secondary_ability { get; set; } } ... var template = new PokemonHTMLView () { Model = detailItem }; webDetailView.LoadHtmlString ( template.GenerateString (), NSBundle.MainBundle.BundleUrl); @model Pokemon <html> <head> <link type="text/css" rel="stylesheet" href="style.css" /> </head> <body> <div class="container"> <div class="header">Type</div> <table class="data"> <tr> <td><b>Primary:</b></td> <td>@Model.primary_type</td> </tr> <tr> <td><b>Secondary:</b></td> <td>@Model.secondary_type</td> </tr> </table> </div> ...
  • 14. No ferrets were harmed in the making of this app.
  • 15. There are several ways to enable interaction between the native part of your application and the HTML view. Hybrid Communication Techniques
  • 16. Expose a C# function to JavaScript <div onclick="Foo.bar('This is a test!')" class="button">Test</div> class Foo : Java.Lang.Object { ... [Export ("bar")] public void Bar (Java.Lang.String message) { Toast.MakeText (context, "Message called from JavaScript: " + message, ToastLength.Short).Show (); } } ... WebView web = FindViewById<WebView> (Resource.Id.myWeb); web.Settings.JavaScriptEnabled = true; web.AddJavascriptInterface (new Foo (this), "Foo"); web.LoadUrl("file:///android_asset/main.html");
  • 17. Intercept a Link Handler <a href="message:This is a test!">Test</a> private class CustomWebViewClient : WebViewClient { public override bool ShouldOverrideUrlLoading (WebView view, string url) { if (url.StartsWith ("message:")) { var message = Uri.UnescapeDataString(url.Split(new char[] { ':' }, 2)[1]); Toast.MakeText (view.Context, "Message called from JavaScript: " + message, ToastLength.Short).Show (); return true; } else return false; } } ... WebView web = FindViewById<WebView> (Resource.Id.myWeb); web.SetWebViewClient (new CustomWebViewClient ()); web.Settings.JavaScriptEnabled = true; web.LoadUrl("file:///android_asset/main.html");
  • 18. Call JavaScript from C# WebView web = FindViewById<WebView> (Resource.Id.myWeb); web.Settings.JavaScriptEnabled = true; web.LoadUrl("file:///android_asset/main.html"); Button button = FindViewById<Button> (Resource.Id.button1); button.Click += (object sender, EventArgs e) => { web.LoadUrl("javascript:document.getElementById('button').innerHTML = 'Hello!'"); };
  • 19. Q&A