Cross platform mobile apps using .NET

7,980 views
7,730 views

Published on

With mobile taking off in a big way it is a fun and exciting time to be a software developer. The landscape is rapidly changing, with a wide variety of options for platforms and programming languages. Businesses are faced with tough decisions on how to provide a best possible user experience, yet keeping maintenance cost down across the different smart phone platforms.

Some are compromising user experience and betting on web based interfaces, while others require the high fidelity user experience or device integration traditionally only found in native apps.

In this session I will demonstrate how we can write fully native applications taking full advantage of the platform, yet achieving a high level of code reuse across Windows Phone 7, Android and iOS.

Topics covered:
- Separated Presentation patterns for maximum code reuse across all platforms
- How to structure your source code and build it for all platforms
- How to access devices specific functionality like Camera, GPS and Accelerometer in a cross platform way
- Tips, tricks and tools to make the cross platform development process smoother.

For maximum benefit from this session some prior knowledge of MVVM is beneficial.

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
7,980
On SlideShare
0
From Embeds
0
Number of Embeds
3,102
Actions
Shares
0
Downloads
83
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Cross platform mobile apps using .NET

  1. 1. Cross platform mobile apps using .NET<br />Jonas Follesø<br />NDC, June 2011<br />
  2. 2. AGENDA:<br /><ul><li>Why?
  3. 3. 8 techniques
  4. 4. Complete example</li></ul>http://www.flickr.com/photos/20792787@N00/2248623391/<br />
  5. 5.
  6. 6.
  7. 7. TILE FLOOD<br />
  8. 8. TILE FLOOD<br />
  9. 9.
  10. 10. Why cross platform..?<br />
  11. 11. REUSE SKILL SET?<br />
  12. 12. REUSE CODE?<br />privatevoidopen_Click(objectsender, RoutedEventArgse)<br />{<br />varchannel=HttpNotificationChannel.Find("NNUG");<br />if (channel==null)<br /> {<br />channel=newHttpNotificationChannel("NNUG"); <br /> }<br />channel.ChannelUriUpdated+=channel_ChannelUriUpdated;<br />channel.ShellToastNotificationReceived+=<br />channel_ShellToastNotificationReceived;<br />channel.Open();<br />}<br />
  13. 13. REUSE LIBRARIES<br /><ul><li>+35 libraries monotouch.info/MonoTouch/Libraries</li></li></ul><li>COST OF MAINTAINING MULTIPLE CODE BASES<br />
  14. 14. LIKE C#..?<br />
  15. 15. ... OR DISLIKE THE ALTERNATIVE<br />
  16. 16. OBJECTIVE-C: DATE (NO TIME)<br />+ (NSDate *) stripTime:(NSDate *) date {NSCalendar *gregorian = [[NSCalendaralloc] initWithCalendarIdentifier:NSGregorianCalendar];NSDateComponents *components =<br /> [gregorian components:<br /> (NSYearCalendarUnit | NSMonthCalendarUnit |<br />NSDayCalendarUnit)<br />fromDate:date]; date = [gregoriandateFromComponents:components]; [gregorian release]; return date;}<br />
  17. 17. C#: DATE (NO TIME)<br />public DateTimeStripTime(DateTime date){ return date.Date;}<br />
  18. 18.
  19. 19. TECHNIQUE 1:PORTABLE CLASS LIBRARY<br />
  20. 20. HELP YOU STICK TO A COMMON SUBSET<br />
  21. 21. PORTABLE CLASS LIBRARY<br />PROS<br /><ul><li>One class library for all platforms
  22. 22. Helps you stick to a common subset</li></ul>CONS<br /><ul><li>No Preprocessor Directives
  23. 23. No Platform Specific Code in Class Lib
  24. 24. Not directly supported on MonoDevelop yet</li></li></ul><li>TECHNIQUE 2:LINKED FILES<br />
  25. 25. WORKS IN MONODEVELOP<br />
  26. 26. LINKED FILES<br />PROS<br /><ul><li>Same code across all platforms
  27. 27. Ability to add platform specific code
  28. 28. Preprocessor Directives</li></ul>CONS<br /><ul><li>Need to manage multiple projects</li></li></ul><li>TECHNIQUE 3:LINK FILES USING «PROJECT LINKER»<br />
  29. 29. PROJECT LINKER<br />PROS<br /><ul><li>Auto link files between projects</li></ul>CONS<br /><ul><li>Does not work in MonoDevelop
  30. 30. Must have all projects in same solution</li></li></ul><li>TECHNIQUE 4:VSMonoTouch Open MonoTouch projects in VS2010<br />
  31. 31. VSMonoTouch<br />PROS<br /><ul><li>Open and build (verify) MonoTouch in VS2010</li></ul>CONS<br /><ul><li>Not a «real» build
  32. 32. Some manual steps involved</li></li></ul><li>TECHNIQUE 5:PREPROCESSOR DIRECTIVES<br />
  33. 33. using System.Net;<br />#if MONOTOUCH || MONODROID<br />using System.Web;<br />#endif<br />
  34. 34. PREPROCESSOR DIRECTIVES<br />PROS<br /><ul><li>Easily write platform specific code</li></ul>CONS<br /><ul><li>Code smell
  35. 35. DRY – The #ifdefs tend to spread</li></li></ul><li>demo<br />
  36. 36. TECHNIQUE 6:ABSTRACT COMMONFUNCTIONALITY<br />
  37. 37. publicinterfaceIDispatchOnUIThread<br />{<br />void Invoke(Action action);<br />}<br />
  38. 38. // Windows Phone 7<br />publicclassDispatchAdapter : IDispatchOnUIThread<br />{<br />publicvoid Invoke(Action action)<br /> {<br />Deployment.Current.Dispatcher.BeginInvoke(action);<br /> }<br />}<br />
  39. 39. // Mono for Android<br />publicclassDispatchAdapter : IDispatchOnUIThread<br />{<br />privatereadonlyActivity _owner;<br />public DispatchAdapter(Activity owner)<br /> {<br /> _owner = owner;<br /> }<br />publicvoid Invoke(Action action)<br /> {<br /> _owner.RunOnUiThread(action);<br /> }<br />}<br />
  40. 40. // Monotouch<br />publicclassDispatchAdapter : IDispatchOnUIThread<br />{<br /> privatereadonly NSObject _owner;<br /> publicDispatchAdapter(NSObject owner)<br /> {<br /> _owner = owner;<br /> }<br /> publicvoid Invoke (Action action)<br /> {<br /> _owner.BeginInvokeOnMainThread(newNSAction(action));<br />}<br />}<br />
  41. 41. ABSTRACT FUNCTIONALITY<br />PROS<br /><ul><li>Share even more code
  42. 42. Do not need preprocessor directives</li></ul>CONS<br /><ul><li>More abstractions in code</li></li></ul><li>TECHNIQUE 7:LEVERAGE MVVM ON ALL PLATFORMS<br />
  43. 43. Ensure that any code that manipulates presentation only manipulates presentation, pushing all domain and data source logic into clearly separated areas of the program.<br />Martin Fowler, Separated Presentation, July 2006<br />
  44. 44.
  45. 45. Separated Presentation Patterns<br />
  46. 46. Data & Domain Logic(Model)<br />UI(View)<br />Interaction (Controller/Presenter)<br />
  47. 47. MVVMPATTERN FOR BINDABLE UI<br />
  48. 48. APP LOGIC IN CODE BEHIND IS HARD TO TEST AND MAINTAIN<br />VIEW<br />XAML<br />DataModel<br />Code-Behind<br />EventHandlers<br />
  49. 49. SEPARATE USINGMVVM<br />VIEW<br />XAML<br />Code-Behind<br />Change notification<br />Data-binding and commands<br />VIEW MODEL<br />Data Model<br />State + Operations<br />
  50. 50. publicclassObservableAdapter<T> : BaseAdapter<T><br />{<br />privatereadonlyActivity _context;<br />privatereadonlyObservableCollection<T> _collection;<br />publicObservableAdapter(Activitycontext, ObservableCollection<T> collection)<br /> {<br /> _context = context;<br /> _collection = collection;<br /> _collection.CollectionChanged += (o, e) => NotifyDataSetChanged();<br />}<br />...<br />ListAdapter = newObservableAdapter<Airport> (this, _viewModel.Airports);<br />
  51. 51. publicabstractclassObservableDataSource<T> : UITableViewDataSource<br />{<br /> publicObservableDataSource (ObservableCollection<T> collection, UITableView tableView)<br /> {<br /> _tableView = tableView;<br /> _collection = collection;<br /> _collection.CollectionChanged += (o, e) => {<br /> tableView.ReloadData();<br /> };<br /> }<br />...<br />
  52. 52. APPLYING MVVM<br />PROS<br /><ul><li>Share even more code
  53. 53. Testable View Models</li></ul>CONS<br /><ul><li>More abstractions in code
  54. 54. May not feel natural to platform</li></li></ul><li>demo<br />
  55. 55. TECHNIQUE 8:USE PRE-BUILT ABSTRACTIONS <br />
  56. 56. PHONE SPECIFIC APIS?<br />http://www.flickr.com/photos/howzey/2880455762/<br />
  57. 57. EXAMPLES<br /><ul><li>Accelerometer
  58. 58. Camera
  59. 59. Compass
  60. 60. Geolocation
  61. 61. Notification
  62. 62. Storage
  63. 63. Contacts
  64. 64. And more...</li></li></ul><li>MonoMobile.Extensions<br /><ul><li>https://github.com/chrisntr/MonoMobile.Extensions
  65. 65. One common API for phone specific functionality across all three devices
  66. 66. Inspired by PhoneGap(same API)</li></ul>http://www.flickr.com/photos/tambako/3974809361/<br />
  67. 67. privatevoid FindNearestAirport()<br />{<br />var coordinates = new MonoMobile.Extensions.Geolocation();<br /> coordinates.GetCurrentPosition(PositionAvailable);<br />}<br />privatevoid PositionAvailable(Position position)<br />{<br />Console.WriteLine("{0} {1}",<br /> position.Coords.Latitude,<br /> position.Coords.Longitude);<br />}<br />
  68. 68. privatevoid FindNearestAirport()<br />{<br />var coordinates = newMonoMobile.Extensions.Geolocation();<br /> coordinates.GetCurrentPosition(PositionAvailable);<br />}<br />privatevoid PositionAvailable(Position position)<br />{<br />Console.WriteLine("{0} {1}",<br /> position.Coords.Latitude,<br /> position.Coords.Longitude);<br />}<br />
  69. 69. 3x MonoMobile.Extensions.dll<br /><ul><li>MonoDroid -> MonoMobile.Extensions.dll
  70. 70. Implemented using LocationManager
  71. 71. MonoTouch -> MonoMobile.Extensions.dll
  72. 72. Implemented using CLLocationManager
  73. 73. Windows Phone 7 -> MonoMobile.Extensions.dll
  74. 74. Implemented using GeoLocationWatcher</li></li></ul><li>ABSTRACT FUNCTIONALITY<br />PROS<br /><ul><li>Share even more code
  75. 75. Same set of abstractions used in multiple apps
  76. 76. Well known API</li></ul>CONS<br /><ul><li>More abstractions in code
  77. 77. Smallest common denominator</li></li></ul><li>DOES IT WORK IN PRACTICE?<br />http://www.flickr.com/photos/kozloski/2306510520/<br />
  78. 78. FLIGHTS NORWAYMONITORARRIVALS AND DEPARTURES<br />http://www.flickr.com/photos/svenwerk/2181849280/<br />
  79. 79.
  80. 80. demo<br />
  81. 81. SUMMARY<br />Portable Library<br />Linked Files<br />Project Linker<br />VSMonoTouch<br />Preprocessor Directives<br />Abstracting Common Functionality<br />Leverage MVVM<br />Using Pre-built Abstractions<br />
  82. 82. FORK ME ON GITHUB<br />github.com/follesoe/FlightsNorway<br /><ul><li>Complete app for iOS, Android & WP7
  83. 83. 56 page tutorial with step-by-step code.</li></li></ul><li>Jonas Follesø<br />Scientist<br />+47 977 06660<br />Jonas.folleso@bekk.nojonas@follesoe.no<br />

×