wp7, droid, iphone, oh my!
DESCRIPTION
Slides for the DDD8a Modern .Net day on MonoTouch, MonoDroid and Windows Phone 7. The videos are up on YouTube and are talked over "live" so apologies if they don't make too much sense!TRANSCRIPT
WP7, iPhone, AndroidOh my!
Chris Hardy
Modern .NET dayReading 23rd October 2010
Chris Hardy
• ASPInsider
• Mobile is my hobby
• http://twitter.com/chrisntr
Covering...
• Windows Phone 7
• MonoTouch
• MonoDroid
• Code reuse
• Porting code
• Demos
Windows Phone 7
• Windows Only
• Visual Studio 2010 / Expression Blend 4
• Silverlight for Windows Phone...
• Great information for developers
MonoTouch
• Mac only
• MonoDevelop
• Apple’s Interface Builder
• Superset of Silverlight 3
• Good community around
MonoDroid(currently in preview form)
• Cross platform
• Visual Studio 2010 / MonoDevelop
• XML Files / DroidDraw
• Superset of Silverlight 3
• Early days
Windows Phone 7
• Metro
• Panorama/Pivot
• Launchers/Choosers
• Tombstoning
Metro
http://crocusgirl.wordpress.com/
Panorama/Pivot
Launchers/Choosers• CameraCaptureTask
• EmailAddressChooserTask
• MarketplaceHubTask
• PhoneCallTask
• PhotoChooserTask
• SearchTask
• SmsComposeTask
Tombstoning
sort of multi-tasking
Demo
Twitter on Windows Phone 7
http://www.youtube.com/watch?v=CZcS5PYG9kw
Twitter on Windows Phone 7
What is MonoTouch?
What is Mono?
The .Net Frameworkcreated by Miguel De Icaza
et al.
Miguel de Icaza
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
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
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...
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);
}
Linkerand the application size...
Linking Assemblies
App-store MonoTouch Minimum App Size
~6MB
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
MonoTouch Events
• Supports Objective-C pattern:
webView.Delegate = new MyWebViewDelegate();
• C# style events as well:
webView.PageLoaded += delegate {HideSpinningWheel();
}
Debugging
• Console.WriteLine(“Debugging inside of MonoTouch”);
• printf(“I made it to this line!\n”)
Debugger
• MonoTouch debugger leverages Mono’s new Soft-Debugger
• Supports the Simulator
• Supports the Device...
• even over WiFi
Professional iPhone Programming with
MonoTouch and .NET/C#
Wallace B. McClureRory Blyth
Martin BowlingCraig Dunn
and me... Chris Hardy
Out Now!
Demo
Twitter on iPhone with MonoTouch
Twitter on iPhone with MonoTouch
Will probably do a video in the future :)
What is MonoDroid?
What is MonoDroid?
What is MonoDroid?
C# on Android
What is MonoDroid?
A commercial product
What is MonoDroid?
Preview releaseNot AlphaNot Beta
What is MonoDroid?
Cross-platform
What is MonoDroid?
Sign-up for preview access
http://go-mono.com/monodroid/
Demo
Twitter on Android with MonoDroid
Twitter on Android with MonoDroid
http://www.youtube.com/watch?v=TKXgugOj56I
Application Stores
Windows Phone 7$99 Per Year
iPhone$99 Per Year
Android$25 Registration
Code reuse
Code reuse
Json.Net
FlickrNet
XNATouch
Rebuild your libraries
• Can’t just use any DLL
• Re-compile for each lib
• Each framework has its own class library
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); } }
Limited WebClient calls
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);
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));
Porting your code
• MOMA for Mobile?
Conclusions
Thanks! Any questions?twitter.com/[email protected]