Transcript
Page 1: WP7, Droid, iPhone, Oh my!

WP7, iPhone, AndroidOh my!

Chris Hardy

Modern .NET dayReading 23rd October 2010

Page 2: WP7, Droid, iPhone, Oh my!

Chris Hardy

• ASPInsider

• Mobile is my hobby

• http://twitter.com/chrisntr

Page 3: WP7, Droid, iPhone, Oh my!

http://greatfridays.com

Page 5: WP7, Droid, iPhone, Oh my!

http://dominion-funds.com

Page 6: WP7, Droid, iPhone, Oh my!

Covering...

• Windows Phone 7

• MonoTouch

• MonoDroid

• Code reuse

• Porting code

• Demos

Page 7: WP7, Droid, iPhone, Oh my!

Windows Phone 7

• Windows Only

• Visual Studio 2010 / Expression Blend 4

• Silverlight for Windows Phone...

• Great information for developers

Page 8: WP7, Droid, iPhone, Oh my!

MonoTouch

• Mac only

• MonoDevelop

• Apple’s Interface Builder

• Superset of Silverlight 3

• Good community around

Page 9: WP7, Droid, iPhone, Oh my!

MonoDroid(currently in preview form)

• Cross platform

• Visual Studio 2010 / MonoDevelop

• XML Files / DroidDraw

• Superset of Silverlight 3

• Early days

Page 10: WP7, Droid, iPhone, Oh my!

Windows Phone 7

• Metro

• Panorama/Pivot

• Launchers/Choosers

• Tombstoning

Page 11: WP7, Droid, iPhone, Oh my!

Metro

http://crocusgirl.wordpress.com/

Page 12: WP7, Droid, iPhone, Oh my!

Panorama/Pivot

Page 13: WP7, Droid, iPhone, Oh my!

Launchers/Choosers• CameraCaptureTask

• EmailAddressChooserTask

• MarketplaceHubTask

• PhoneCallTask

• PhotoChooserTask

• SearchTask

• SmsComposeTask

Page 14: WP7, Droid, iPhone, Oh my!

Tombstoning

sort of multi-tasking

Page 15: WP7, Droid, iPhone, Oh my!

Demo

Twitter on Windows Phone 7

Page 16: WP7, Droid, iPhone, Oh my!

http://www.youtube.com/watch?v=CZcS5PYG9kw

Twitter on Windows Phone 7

Page 17: WP7, Droid, iPhone, Oh my!

What is MonoTouch?

Page 18: WP7, Droid, iPhone, Oh my!

What is Mono?

The .Net Frameworkcreated by Miguel De Icaza

et al.

Page 19: WP7, Droid, iPhone, Oh my!

Miguel de Icaza

Page 20: WP7, Droid, iPhone, Oh my!

Mono’s JIT Engine

CIL

Mono

Memory

Page 21: WP7, Droid, iPhone, Oh my!

Apple Rules

• Contractual Requirements

• No interpreted code

• No shared libraries

• Kernel Limitations

• iPhone OS 2.0+ disables JIT

Page 22: WP7, Droid, iPhone, Oh my!

Mono’s AOT Engine

CIL

Mono

AOTMono Runtime

ARM

Native Code

Page 23: WP7, Droid, iPhone, Oh my!

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 24: WP7, Droid, iPhone, Oh my!

MonoTouch’s APIs

Page 25: WP7, Droid, iPhone, Oh my!

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 26: WP7, Droid, iPhone, Oh my!

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 27: WP7, Droid, iPhone, Oh my!

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 28: WP7, Droid, iPhone, Oh my!

Linkerand the application size...

Page 29: WP7, Droid, iPhone, Oh my!

Linking Assemblies

Page 30: WP7, Droid, iPhone, Oh my!

App-store MonoTouch Minimum App Size

~6MB

Page 31: WP7, Droid, iPhone, Oh my!

Integration

• Tight integration between MD and IB

• IB produces XIBs with MD parses

• Automatic class generation in MD

• Generates partial classes for all types, outlets and actions defined in Interface Builder

Page 32: WP7, Droid, iPhone, Oh my!

MonoTouch Events

• Supports Objective-C pattern:

webView.Delegate = new MyWebViewDelegate();

• C# style events as well:

webView.PageLoaded += delegate {HideSpinningWheel();

}

Page 33: WP7, Droid, iPhone, Oh my!

Debugging

• Console.WriteLine(“Debugging inside of MonoTouch”);

• printf(“I made it to this line!\n”)

Page 34: WP7, Droid, iPhone, Oh my!

Debugger

• MonoTouch debugger leverages Mono’s new Soft-Debugger

• Supports the Simulator

• Supports the Device...

• even over WiFi

Page 36: WP7, Droid, iPhone, Oh my!

Demo

Twitter on iPhone with MonoTouch

Page 37: WP7, Droid, iPhone, Oh my!

Twitter on iPhone with MonoTouch

Will probably do a video in the future :)

Page 38: WP7, Droid, iPhone, Oh my!

What is MonoDroid?

Page 39: WP7, Droid, iPhone, Oh my!

What is MonoDroid?

Page 40: WP7, Droid, iPhone, Oh my!

What is MonoDroid?

C# on Android

Page 41: WP7, Droid, iPhone, Oh my!

What is MonoDroid?

A commercial product

Page 42: WP7, Droid, iPhone, Oh my!

What is MonoDroid?

Preview releaseNot AlphaNot Beta

Page 43: WP7, Droid, iPhone, Oh my!

What is MonoDroid?

Cross-platform

Page 44: WP7, Droid, iPhone, Oh my!

What is MonoDroid?

Sign-up for preview access

http://go-mono.com/monodroid/

Page 45: WP7, Droid, iPhone, Oh my!

Demo

Twitter on Android with MonoDroid

Page 46: WP7, Droid, iPhone, Oh my!

Twitter on Android with MonoDroid

http://www.youtube.com/watch?v=TKXgugOj56I

Page 47: WP7, Droid, iPhone, Oh my!

Application Stores

Page 48: WP7, Droid, iPhone, Oh my!

Windows Phone 7$99 Per Year

iPhone$99 Per Year

Android$25 Registration

Page 49: WP7, Droid, iPhone, Oh my!

Code reuse

Page 50: WP7, Droid, iPhone, Oh my!

Code reuse

Json.Net

FlickrNet

XNATouch

Page 51: WP7, Droid, iPhone, Oh my!

Rebuild your libraries

• Can’t just use any DLL

• Re-compile for each lib

• Each framework has its own class library

Page 52: WP7, Droid, iPhone, Oh my!

Not an abstraction!

Page 53: WP7, Droid, iPhone, Oh my!

Not an abstraction!

• Platform specific APIs

• Platform specific look and feel

Page 54: WP7, Droid, iPhone, Oh my!

Mono for WP7?

Page 55: WP7, Droid, iPhone, Oh my!

Mono for WP7?

• Look at Mono code and copy over!

• IQueryable support in WP7

• More later...

Page 56: WP7, Droid, iPhone, Oh my!

Porting over Hanselminutes

Page 57: WP7, Droid, iPhone, Oh my!

Porting over Hanselminutes

hanselminutesiphone.codeplex.com

Page 58: WP7, Droid, iPhone, Oh my!

Porting over Hanselminutes

• Make sure your business logic is separate

• Use actions for returning results

Page 59: WP7, Droid, iPhone, Oh my!

Getting XML

Page 60: WP7, Droid, iPhone, Oh my!

Persisting the XML

Page 61: WP7, Droid, iPhone, Oh my!

Isolated Storagevs

File Storage

Page 62: WP7, Droid, iPhone, Oh my!

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 63: WP7, Droid, iPhone, Oh my!

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 64: WP7, Droid, iPhone, Oh my!

No File.WriteAllText?

No problem!

Page 65: WP7, Droid, iPhone, Oh my!

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 66: WP7, Droid, iPhone, Oh my!

Limited WebClient calls

Page 67: WP7, Droid, iPhone, Oh my!

Downloading a file - MonoTouch

_webClient = new WebClient ();_webClient.DownloadFileCompleted += (sender, e) =>{ _fileDownloaded = true; if (_downloadCompletedSuccessfully) { File.Copy (_show.PartialAudioPath, _show.OfflineAudioPath); } UIHelper.StopInternetActivity ();};

_webClient.DownloadProgressChanged += HandleClientDownloadProgressChanged;

_webClient.DownloadFileAsync (new Uri (_show.Url), _show.PartialAudioPath);

Page 68: WP7, Droid, iPhone, Oh my!

Downloading a file WP7var client = new WebClient();client.DownloadProgressChanged += (s, e) =>{ progressBar1.Value = e.ProgressPercentage / 100;};

client.OpenReadCompleted += (s, e) =>{ using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())

using (var fs = new IsolatedStorageFileStream(CurrentShow.OfflineAudioPath, FileMode.Create, isoStore)) { int bytesRead; byte[] bytes = new byte[1024 * 1024 * 1]; // 1meg while ((bytesRead = e.Result.Read(bytes, 0, bytes.Length)) != 0) { fs.Write(bytes, 0, bytesRead); } _fileDownloaded = true; fs.Flush(); }};client.OpenReadAsync(new Uri(CurrentShow.Url, UriKind.Absolute));

Page 69: WP7, Droid, iPhone, Oh my!

Porting your code

• MOMA for Mobile?

Page 70: WP7, Droid, iPhone, Oh my!

Conclusions


Top Related