Slide 1

Slide 1 text

Cross-platform Mobile Applications with C# and .NET Miracle Open World 2012 19th April 2012 There will be QR Codes, be ready!

Slide 2

Slide 2 text

Chris Hardy • .NET Contractor • Work with Xamarin • Wrote some books • First spoke about MonoTouch in January 2010 • http://twitter.com/chrisntr

Slide 3

Slide 3 text

Agenda • What is Mono? • .NET on iOS with MonoTouch • .NET on Android with Mono for Android • Sharing .NET code

Slide 4

Slide 4 text

Where it all begins...

Slide 5

Slide 5 text

What is Mono? • Open source implementation of the .NET platform created by Ximian • 2001: Created to bring Windows applications to Linux • 2003: Acquired by Novell with Ximian • Matured at Novell in research mode; now a world-class runtime.

Slide 6

Slide 6 text

C# IronRuby IronPython Visual Basic F#

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Xamarin • Xamarin founded in May 2011 • Xamarin has a perpetual license to all Mono IP: copyrights, patents and trademarks • Focused on mobile app development

Slide 9

Slide 9 text

C# IronRuby IronPython Visual Basic F#

Slide 10

Slide 10 text

MonoTouch Mono for Android

Slide 11

Slide 11 text

What is MonoTouch? • C# on iOS devices (iPhone, iPod Touch, iPad) What MonoTouch is not... • Silverlight for iOS devices • Compact Framework for iOS devices • iOS development without a Mac • A meta-platform

Slide 12

Slide 12 text

What is Mono for Android? • C# on Android devices What Mono for Android is not... • Silverlight for Android devices • Compact Framework for Android devices • Supported for Linux • A meta-platform

Slide 13

Slide 13 text

xamarin.com/apps

Slide 14

Slide 14 text

Demo Tweeting with MonoTouch

Slide 15

Slide 15 text

Demo of Mono for Android

Slide 16

Slide 16 text

MonoTouch / Mono for Android Features • MonoDevelop iPhone/Android Add-In • Visual Studio support for Android • monotouch.dll / monoandroid.dll • Full static AOT compiler / Allow JIT for Android • Support for all your existing code • Reflection • Generics • LINQ • Anonymous Methods • Lambda’s etc...

Slide 17

Slide 17 text

MonoTouch APIs .NET APIs Native APIs 3rd Party mscorlib System System.Core (LINQ) System.Data Mono.Data.Sqlite System.ServiceModel System.Json System.Web.Services (WCF) System.Xml System.Xml.Linq AddressBook/ AddressBookUI AudioToolbox/ AVFoundation CoreAnimation CoreGraphics CoreLocation EventKit/EventKitUI ExternalAccessory GameKit MapKit NewsstandKit StoreKit Twitter UIKit And so on.... OpenTK OpenGL OpenAL SQLite MonoGame RestSharp Json.NET ServiceStack Steema TeeChart Flurry Analytics RedLaser And so on...

Slide 18

Slide 18 text

Mono for Android APIs .NET APIs Native APIs 3rd Party mscorlib System System.Core (LINQ) System.Data Mono.Data.Sqlite System.ServiceModel System.Json System.Web.Services (WCF) System.Xml System.Xml.Linq Android.Accounts Android.App.* Android.Bluetooth Android.Content.* Android.Database Android.Graphics Android.Hardware Android.Locations Android.Net.* Android.Nfc Android.Text Android.Util Android.Views.* Android.Widget Java.Lang.* Java.Util.* And so on.... OpenTK OpenGL OpenAL SQLite MonoGame RestSharp Json.NET ServiceStack Steema TeeChart And so on...

Slide 19

Slide 19 text

Cross platform with MonoTouch, Mono for Android, WP7

Slide 20

Slide 20 text

Win/WP7 WPF Silverlight OSX MonoMac C# Plus ECMA languages .NET iOS MonoTouch Android Mono for Android Mono Business Logic and Middleware (engine, core and 3rd party) Native UI APIs Runtime OS Code sharing and native experience

Slide 21

Slide 21 text

MWC 2012 http://bit.ly/mwc2012xamarin

Slide 22

Slide 22 text

MWC 2012

Slide 23

Slide 23 text

MWC 2012 • 1,635 Lines of shared code - 771 for ORM

Slide 24

Slide 24 text

MWC 2012 • 1,635 Lines of shared code - 771 for ORM • iPhone + iPad (2,476 LOC / 2 Apps) - 57% Re-use

Slide 25

Slide 25 text

MWC 2012 • 1,635 Lines of shared code - 771 for ORM • iPhone + iPad (2,476 LOC / 2 Apps) - 57% Re-use • Android (1,095 LOC) - 60% Re-use

Slide 26

Slide 26 text

MWC 2012 • 1,635 Lines of shared code - 771 for ORM • iPhone + iPad (2,476 LOC / 2 Apps) - 57% Re-use • Android (1,095 LOC) - 60% Re-use • Windows Phone 7 (896 LOC) - 65% Re-use

Slide 27

Slide 27 text

Techniques for Cross Platform Code

Slide 28

Slide 28 text

Techniques for Cross Platform Code 1. Portable Library Tools

Slide 29

Slide 29 text

Techniques for Cross Platform Code 1. Portable Library Tools 2. Linking Files

Slide 30

Slide 30 text

Techniques for Cross Platform Code 1. Portable Library Tools 2. Linking Files 3. Project Linker

Slide 31

Slide 31 text

Techniques for Cross Platform Code 1. Portable Library Tools 2. Linking Files 3. Project Linker 4. Xamarin.Mobile

Slide 32

Slide 32 text

Portable Library Tools http://bit.ly/portablelibrarytools

Slide 33

Slide 33 text

Portable Library Tools

Slide 34

Slide 34 text

Portable Library Tools • Pros

Slide 35

Slide 35 text

Portable Library Tools • Pros • One class library to rule them all

Slide 36

Slide 36 text

Portable Library Tools • Pros • One class library to rule them all • Common subset across all platforms

Slide 37

Slide 37 text

Portable Library Tools • Pros • One class library to rule them all • Common subset across all platforms • Cons

Slide 38

Slide 38 text

Portable Library Tools • Pros • One class library to rule them all • Common subset across all platforms • Cons • No Pre-processor Directives

Slide 39

Slide 39 text

Portable Library Tools • Pros • One class library to rule them all • Common subset across all platforms • Cons • No Pre-processor Directives • No platform specific-code

Slide 40

Slide 40 text

Portable Library Tools • Pros • One class library to rule them all • Common subset across all platforms • Cons • No Pre-processor Directives • No platform specific-code • Limited API set - no System.Xml.Linq or System.Net.WebClient

Slide 41

Slide 41 text

Demo Telephone Converter

Slide 42

Slide 42 text

Linking Files

Slide 43

Slide 43 text

Demo Twitter App

Slide 44

Slide 44 text

Project Linker http://bit.ly/projectlinkervs

Slide 45

Slide 45 text

Project Linker

Slide 46

Slide 46 text

Project Linker • Pros

Slide 47

Slide 47 text

Project Linker • Pros • Auto-link files between projects

Slide 48

Slide 48 text

Project Linker • Pros • Auto-link files between projects • Cons

Slide 49

Slide 49 text

Project Linker • Pros • Auto-link files between projects • Cons • Does not work with MonoDevelop

Slide 50

Slide 50 text

Project Linker • Pros • Auto-link files between projects • Cons • Does not work with MonoDevelop • Must have all projects in the same solution

Slide 51

Slide 51 text

Xamarin.Mobile

Slide 52

Slide 52 text

http://xamarin.com/mobileapi

Slide 53

Slide 53 text

StringBuilder builder = new StringBuilder(); String lineSep = System.getProperty("line.separator"); ContentResolver content = getContentResolver(); Cursor ncursor = null; try { ncursor = content.query (ContactsContract.Data.CONTENT_URI, new String[] { ContactsContract.Data.MIMETYPE, ContactsContract.Contacts.LOOKUP_KEY, ContactsContract.Contacts.DISPLAY_NAME }, ContactsContract.Data.MIMETYPE + "=? AND " + ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME + "=?", new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE, "Eric" }, null); while (ncursor.moveToNext()) { builder.append (ncursor.getString(ncursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)) + lineSep); String lookupKey = ncursor.getString (ncursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); Cursor dcursor = null; try { dcursor = content.query (ContactsContract.Data.CONTENT_URI, new String[] { ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.Data.DATA1 }, ContactsContract.Contacts.LOOKUP_KEY + "=?", new String[] { lookupKey }, null); while (dcursor.moveToNext()) { String type = dcursor.getString (ncursor.getColumnIndex(ContactsContract.Data.MIMETYPE)); if (type.equals (ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) builder.append ("Phone: " + dcursor.getString(dcursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)) + lineSep); else if (type.equals (ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)) builder.append ("Email: " + dcursor.getString(dcursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA1)) + lineSep); } builder.append(lineSep); } finally { if (dcursor != null) dcursor.close(); } } } finally { if (ncursor != null) ncursor.close(); } t.setText(builder.toString());

Slide 54

Slide 54 text

var builder = new StringBuilder(); var abook = new AddressBook(this); foreach (Contact c in abook.Where (c => c.FirstName == "Eric" && c.Phones.Any())) { builder.AppendLine (c.DisplayName); foreach (Phone p in c.Phones) builder.AppendLine (String.Format ("{0}: {1}", p.Label, p.Number)); builder.AppendLine(); } Console.WriteLine (builder.ToString());

Slide 55

Slide 55 text

StringBuilder builder = new StringBuilder(); String lineSep = System.getProperty("line.separator"); ContentResolver content = getContentResolver(); Cursor ncursor = null; try { ncursor = content.query (ContactsContract.Data.CONTENT_URI, new String[] { ContactsContract.Data.MIMETYPE, ContactsContract.Contacts.LOOKUP_KEY, ContactsContract.Contacts.DISPLAY_NAME }, ContactsContract.Data.MIMETYPE + "=? AND " + ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME + "=?", new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE, "Eric" }, null); while (ncursor.moveToNext()) { builder.append (ncursor.getString(ncursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)) + lineSep); String lookupKey = ncursor.getString (ncursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); Cursor dcursor = null; try { dcursor = content.query (ContactsContract.Data.CONTENT_URI, new String[] { ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.Data.DATA1 }, ContactsContract.Contacts.LOOKUP_KEY + "=?", new String[] { lookupKey }, null); while (dcursor.moveToNext()) { String type = dcursor.getString (ncursor.getColumnIndex(ContactsContract.Data.MIMETYPE)); if (type.equals (ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) builder.append ("Phone: " + dcursor.getString(dcursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)) + lineSep); else if (type.equals (ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)) builder.append ("Email: " + dcursor.getString(dcursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA1)) + lineSep); } builder.append(lineSep); } finally { if (dcursor != null) dcursor.close(); } } } finally { if (ncursor != null) ncursor.close(); } t.setText(builder.toString()); Manually in Java

Slide 56

Slide 56 text

var builder = new StringBuilder(); var abook = new AddressBook(this); foreach (Contact c in abook.Where (c => c.FirstName == "Eric" && c.Phones.Any())) { builder.AppendLine (c.DisplayName); foreach (Phone p in c.Phones) builder.AppendLine (String.Format ("{0}: {1}", p.Label, p.Number)); builder.AppendLine(); } Console.WriteLine (builder.ToString()); Xamarin.Mobile for Android

Slide 57

Slide 57 text

var builder = new StringBuilder(); var abook = new AddressBook(); foreach (Contact c in abook.Where (c => c.FirstName == "Eric" && c.Phones.Any())) { builder.AppendLine (c.DisplayName); foreach (Phone p in c.Phones) builder.AppendLine (String.Format ("{0}: {1}", p.Label, p.Number)); builder.AppendLine(); } Console.WriteLine (builder.ToString()); Xamarin.Mobile for iOS

Slide 58

Slide 58 text

var builder = new StringBuilder(); var abook = new AddressBook(); foreach (Contact c in abook.Where (c => c.FirstName == "Eric" && c.Phones.Any())) { builder.AppendLine (c.DisplayName); foreach (Phone p in c.Phones) builder.AppendLine (String.Format ("{0}: {1}", p.Label, p.Number)); builder.AppendLine(); } Console.WriteLine (builder.ToString()); Xamarin.Mobile for Windows Phone 7.1

Slide 59

Slide 59 text

The Future Personal Ideas • MonoTouch support for Visual Studio / Able to open WP7 projects in MonoDevelop • File -> New Cross-Platform Application • One “MonoMobile” class library for iOS, Android and Windows Phone 7 • Analyser tool for existing libraries

Slide 60

Slide 60 text

More resources? • http://docs.xamarin.com • Forums / Mailing Lists • StackOverflow • [email protected] • twitter.com/xamarinhq

Slide 61

Slide 61 text

Creating amazing applications!

Slide 62

Slide 62 text

Q & A Thanks! 15% off of MonoTouch or Mono for Android E-mail [email protected]