Introduction to MonoTouch and Monodroid/Mono for Android

70
MonoTouch and Mono for Android NxtGen User Group Cambridge, Shenfield & Southampton 10th -12th May 2011 with some Windows Phone 7 Chris Hardy

description

Here are the slides used in my NxtGen UK Mini-tour in May 2011

Transcript of Introduction to MonoTouch and Monodroid/Mono for Android

Page 1: Introduction to MonoTouch and Monodroid/Mono for Android

MonoTouch andMono for Android

NxtGen User GroupCambridge, Shenfield & Southampton

10th -12th May 2011

with some Windows Phone 7

Chris Hardy

Page 2: Introduction to MonoTouch and Monodroid/Mono for Android

Chris Hardy

• ASPInsider

• Write some books

• http://twitter.com/chrisntr

• Work for great fridays

Page 3: Introduction to MonoTouch and Monodroid/Mono for Android

http://greatfridays.com

Page 5: Introduction to MonoTouch and Monodroid/Mono for Android

http://dominion-funds.com

Page 6: Introduction to MonoTouch and Monodroid/Mono for Android

Covering...

• How these technologies work

• Demos

• Similarities between MonoTouch and Mono for Android

• Code Re-use with MonoTouch, Mono for Android and Windows Phone 7

Page 7: Introduction to MonoTouch and Monodroid/Mono for Android

MonoTouch

Page 8: Introduction to MonoTouch and Monodroid/Mono for Android

What is Mono?

The .Net Frameworkcreated by Miguel De Icaza

et al.

Page 9: Introduction to MonoTouch and Monodroid/Mono for Android

Miguel de Icaza

Page 10: Introduction to MonoTouch and Monodroid/Mono for Android

What is MonoTouch?

C# and .NET on the iPhone

Page 11: Introduction to MonoTouch and Monodroid/Mono for Android

Demo - Walkthrough

Page 12: Introduction to MonoTouch and Monodroid/Mono for Android

Mono’s JIT Engine

CIL

Mono

Memory

Page 13: Introduction to MonoTouch and Monodroid/Mono for Android

Apple Rules

• Contractual Requirements

• No interpreted code

• No shared libraries

• Kernel Limitations

• iPhone OS 2.0+ disables JIT

Page 14: Introduction to MonoTouch and Monodroid/Mono for Android

Mono’s AOT Engine

CIL

Mono

AOTMono Runtime

ARM

Native Code

Page 15: Introduction to MonoTouch and Monodroid/Mono for Android

MonoTouch Features• mtouch• MonoDevelop iPhone Add-In• CocoaTouch.NET / monotouch.dll• Full static AOT compiler• Support for all your existing code• Reflection• Generics• LINQ• Anonymous Methods• Lambda’s etc...

Page 16: Introduction to MonoTouch and Monodroid/Mono for Android

MonoTouch’s APIs

Page 17: Introduction to MonoTouch and Monodroid/Mono for Android

Strong Types

• Objective-C

• Arrays are weakly typed:• NSArray return values.

• MonoTouch has strong types

• UIView[] Subviews { get; }• vs• NSArray *subviews;

• Intellisense - explore the API...

Page 18: Introduction to MonoTouch and Monodroid/Mono for Android

MonoTouch Events• Supports Objective-C pattern (including

blocks):

webView.Delegate = new MyWebViewDelegate();

• C# style events as well:

webView.PageLoaded += delegate {HideSpinningWheel();

}

Page 19: Introduction to MonoTouch and Monodroid/Mono for Android

Garbage Collection• Automatic:

• Mono’s GC will collect objects on demand

• Deterministic:

• Use when you need control.

• Every object in MonoTouch implements IDisposable

using (var image = UIImage.FromFile(“foo.png”)){

surface.DrawImage(image, 20, 20);

}

Page 20: Introduction to MonoTouch and Monodroid/Mono for Android

MonoTouch Design Integration

• Integrates with Interface Builder and Xcode 3

• Xcode 4 support coming

Page 21: Introduction to MonoTouch and Monodroid/Mono for Android

Demo - Integration

Page 22: Introduction to MonoTouch and Monodroid/Mono for Android

Learn to read Objective-C

Page 23: Introduction to MonoTouch and Monodroid/Mono for Android

Learn to read Objective-C

• All Apple documentation is in Obj-C

• Most examples are in Obj-C

• It’s not too hard to understand

• It might even be fun...

Page 24: Introduction to MonoTouch and Monodroid/Mono for Android

The Bindings• MonoTouch namespace

• MonoTouch.Foo namespace

• Maps to CocoaTouch’s Foo Framework

• 1:1 Mapping of classes.

• MonoTouch.UIKit.UILabel

• CocoaTouch’s UIKit framework, UILabel class

Page 27: Introduction to MonoTouch and Monodroid/Mono for Android

Getting started

• Get iPhone SDK from Apple

• Get Mono from Novell

• Get MonoTouch (evaluation version is free)

• Get MonoDevelop

• Register with Apple iPhone Developer Program and purchase MonoTouch for putting apps on device and AppStore.

Page 28: Introduction to MonoTouch and Monodroid/Mono for Android

DemoTwitter on iPhone with MonoTouch

Page 29: Introduction to MonoTouch and Monodroid/Mono for Android

What is Mono for Android?

Page 30: Introduction to MonoTouch and Monodroid/Mono for Android

What is MonoDroid?

C# and .NET on Android

Page 31: Introduction to MonoTouch and Monodroid/Mono for Android

Demo

Page 32: Introduction to MonoTouch and Monodroid/Mono for Android

How does MonoDroid work?

Page 33: Introduction to MonoTouch and Monodroid/Mono for Android

What is MonoDroid?

Cross-platform

Page 34: Introduction to MonoTouch and Monodroid/Mono for Android

MonoDroid Features• mandroid.exe• Visual Studio 2010 Integration• MonoDevelop MonoDroid Add-In• Mono.Android.dll• Full static AOT compiler and JIT support• Support for all your existing code• Reflection• Generics• LINQ• Anonymous Methods• Lambda’s etc...

Page 35: Introduction to MonoTouch and Monodroid/Mono for Android

What is MonoDroid?

v1.0 released in March

was expected late 2010...

Page 36: Introduction to MonoTouch and Monodroid/Mono for Android

What is MonoDroid?

Expect rapid releases a la MonoTouch

Page 37: Introduction to MonoTouch and Monodroid/Mono for Android

What is MonoDroid?

A commercial product

same prices as MonoTouch

Page 38: Introduction to MonoTouch and Monodroid/Mono for Android

What is MonoDroid?

Give feedback!Submit bugs!

http://monodroid.net/

Page 39: Introduction to MonoTouch and Monodroid/Mono for Android

Mono for Android book on its way!

Out around July

Page 40: Introduction to MonoTouch and Monodroid/Mono for Android

DemoTwitter on Android with MonoDroid

Page 41: Introduction to MonoTouch and Monodroid/Mono for Android

Similarities with MonoTouch and

MonoDroid

Page 42: Introduction to MonoTouch and Monodroid/Mono for Android

Linkerand the application size...

Page 43: Introduction to MonoTouch and Monodroid/Mono for Android

What about App Size?

• 20 MB (compressed) limit on 3G/Edge downloads

• .Net BCL and other libraries are huge

• Mono Linker to the rescue!

Page 44: Introduction to MonoTouch and Monodroid/Mono for Android

Linking Assemblies

Page 45: Introduction to MonoTouch and Monodroid/Mono for Android

Linker Options

• No Link

• Link SDK Only

• Full Link

Page 46: Introduction to MonoTouch and Monodroid/Mono for Android

Same Base Class Libraries

More on this later...

Page 47: Introduction to MonoTouch and Monodroid/Mono for Android

Debugger

• MonoTouch/MonoDroid debugger leverages Mono’s Soft-Debugger

• Supports the Simulator

• Supports the Device - on paid versions

• even over WiFi*

Page 48: Introduction to MonoTouch and Monodroid/Mono for Android

Debugger Features• Breakpoints

• Catchpoints

• Inspection

• Watches

• Immediate / Expression Evaluator

• Call Stack

• Stepping

Page 49: Introduction to MonoTouch and Monodroid/Mono for Android

App-store Minimum MonoTouch App Size

~1MB

Minimum Compressed MonoTouch App Size

~4MB

Page 50: Introduction to MonoTouch and Monodroid/Mono for Android

4.4MB

Minimum MonoDroid App Size (the .apk)

2.2MBCompressed

Page 51: Introduction to MonoTouch and Monodroid/Mono for Android

Code reuse

Page 52: Introduction to MonoTouch and Monodroid/Mono for Android

Code reuse

Json.Net

FlickrNet

XNATouch (Now MonoGame)

Page 53: Introduction to MonoTouch and Monodroid/Mono for Android

Rebuild your libraries

• Can’t just use any DLL

• Re-compile for each lib

• Each framework has its own class library

Page 54: Introduction to MonoTouch and Monodroid/Mono for Android

Portable Library Project

will help

Page 55: Introduction to MonoTouch and Monodroid/Mono for Android

Not an abstraction!

Page 56: Introduction to MonoTouch and Monodroid/Mono for Android

Not an abstraction!

• Platform specific APIs

• Platform specific look and feel

Page 57: Introduction to MonoTouch and Monodroid/Mono for Android

Mono for WP7?

Page 58: Introduction to MonoTouch and Monodroid/Mono for Android

Mono for WP7?

• Look at Mono code and copy over!

• IQueryable support in WP7

• More later...

Page 59: Introduction to MonoTouch and Monodroid/Mono for Android

Porting over Hanselminutes

Page 60: Introduction to MonoTouch and Monodroid/Mono for Android

Porting over Hanselminutes

hanselminutesiphone.codeplex.com

Page 61: Introduction to MonoTouch and Monodroid/Mono for Android

Porting over Hanselminutes

• Make sure your business logic is separate

• Use actions for returning results

Page 62: Introduction to MonoTouch and Monodroid/Mono for Android

Getting XML

Page 63: Introduction to MonoTouch and Monodroid/Mono for Android

Persisting the XML

Page 64: Introduction to MonoTouch and Monodroid/Mono for Android

Isolated Storagevs

File Storage

Page 65: Introduction to MonoTouch and Monodroid/Mono for Android

Storing data void SaveLocal(string data) {#if (MonoTouch || MonoDroid) File.WriteAllText(_localPath, data);#elif WINDOWS_PHONE using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication()) { var file = appStorage.OpenFile(_localPath, FileMode.Create); FileExtension.WriteAllText(file, data); }#endif }

Page 66: Introduction to MonoTouch and Monodroid/Mono for Android

Storing data void SaveLocal(string data) {#if (MonoTouch || MonoDroid) File.WriteAllText(_localPath, data);#elif WINDOWS_PHONE using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication()) { var file = appStorage.OpenFile(_localPath, FileMode.Create); FileExtension.WriteAllText(file, data); }#endif }

Page 67: Introduction to MonoTouch and Monodroid/Mono for Android

No File.WriteAllText?

No problem!

Page 68: Introduction to MonoTouch and Monodroid/Mono for Android

Implementing File.WriteAllText

public static void WriteAllText(IsolatedStorageFileStream fileStream, string data) { using (StreamWriter sw = new StreamWriter(fileStream)) { sw.Write(data); sw.Close(); } }

public static void WriteAllText(string path, string contents) { WriteAllText(path, contents, Encoding.UTF8); }

public static void WriteAllText(string path, string contents, Encoding encoding) { using (StreamWriter sw = new StreamWriter(path, false, encoding)) { sw.Write(contents); } }

Page 69: Introduction to MonoTouch and Monodroid/Mono for Android

Mono Abstractions

With MonoMobile.Extensions in the futurehttps://github.com/chrisntr/MonoMobile.Extensions