Introduction to MonoTouch and Monodroid/Mono for Android

Post on 15-Jan-2015

6.154 views 0 download

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

MonoTouch andMono for Android

NxtGen User GroupCambridge, Shenfield & Southampton

10th -12th May 2011

with some Windows Phone 7

Chris Hardy

Chris Hardy

• ASPInsider

• Write some books

• http://twitter.com/chrisntr

• Work for great fridays

http://greatfridays.com

http://dominion-funds.com

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

MonoTouch

What is Mono?

The .Net Frameworkcreated by Miguel De Icaza

et al.

Miguel de Icaza

What is MonoTouch?

C# and .NET on the iPhone

Demo - Walkthrough

Mono’s JIT Engine

CIL

Mono

Memory

Apple Rules

• Contractual Requirements

• No interpreted code

• No shared libraries

• Kernel Limitations

• iPhone OS 2.0+ disables JIT

Mono’s AOT Engine

CIL

Mono

AOTMono Runtime

ARM

Native Code

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...

MonoTouch’s APIs

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...

MonoTouch Events• Supports Objective-C pattern (including

blocks):

webView.Delegate = new MyWebViewDelegate();

• C# style events as well:

webView.PageLoaded += delegate {HideSpinningWheel();

}

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);

}

MonoTouch Design Integration

• Integrates with Interface Builder and Xcode 3

• Xcode 4 support coming

Demo - Integration

Learn to read Objective-C

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...

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

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.

DemoTwitter on iPhone with MonoTouch

What is Mono for Android?

What is MonoDroid?

C# and .NET on Android

Demo

How does MonoDroid work?

What is MonoDroid?

Cross-platform

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...

What is MonoDroid?

v1.0 released in March

was expected late 2010...

What is MonoDroid?

Expect rapid releases a la MonoTouch

What is MonoDroid?

A commercial product

same prices as MonoTouch

What is MonoDroid?

Give feedback!Submit bugs!

http://monodroid.net/

Mono for Android book on its way!

Out around July

DemoTwitter on Android with MonoDroid

Similarities with MonoTouch and

MonoDroid

Linkerand the application size...

What about App Size?

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

• .Net BCL and other libraries are huge

• Mono Linker to the rescue!

Linking Assemblies

Linker Options

• No Link

• Link SDK Only

• Full Link

Same Base Class Libraries

More on this later...

Debugger

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

• Supports the Simulator

• Supports the Device - on paid versions

• even over WiFi*

Debugger Features• Breakpoints

• Catchpoints

• Inspection

• Watches

• Immediate / Expression Evaluator

• Call Stack

• Stepping

App-store Minimum MonoTouch App Size

~1MB

Minimum Compressed MonoTouch App Size

~4MB

4.4MB

Minimum MonoDroid App Size (the .apk)

2.2MBCompressed

Code reuse

Code reuse

Json.Net

FlickrNet

XNATouch (Now MonoGame)

Rebuild your libraries

• Can’t just use any DLL

• Re-compile for each lib

• Each framework has its own class library

Portable Library Project

will help

Not an abstraction!

Not an abstraction!

• Platform specific APIs

• Platform specific look and feel

Mono for WP7?

Mono for WP7?

• Look at Mono code and copy over!

• IQueryable support in WP7

• More later...

Porting over Hanselminutes

Porting over Hanselminutes

hanselminutesiphone.codeplex.com

Porting over Hanselminutes

• Make sure your business logic is separate

• Use actions for returning results

Getting XML

Persisting the XML

Isolated Storagevs

File Storage

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 }

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 }

No File.WriteAllText?

No problem!

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); } }

Mono Abstractions

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

Q + A

twitter.com/chrisntrchrisntr@gmail.com