Cross-platform Mobile Development

31,092
-1

Published on

Learn about cross-platform approaches to developing native applications using C# and .NET for iOS, Android and Windows Phone 7.

Published in: Technology

Cross-platform Mobile Development

  1. 1. Xamarin Seminar 19th April 2012 Copyright 2012 © Xamarin Inc. All rights reserved
  2. 2. AgendaCross-Platform Mobile Development Greg Shackles Senior Software Engineer OLO Online Ordering greg@gregshackles.com gregshackles.com @gshackles github.com/gshackles Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  3. 3. IntroductionThis session will discuss how to use C# to developiOS, Android and Windows Phone applications.We’ll cover: •Why use C#? •Code sharing techniques and patterns •Useful libraries Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  4. 4. Native Platform Languages Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  5. 5. Write Once, Run Anywhere? != != Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  6. 6. C# to the Rescue! Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  7. 7. Benefits•C# and .NET are mature and powerful•Skill reuse on all platforms•Apps are still completely native•Code can be reused across platforms (even non-mobile platforms!) Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  8. 8. Architecture Mono#for# UI# Silverlight# MonoTouch# Android# C## Business#Logic#Run)me# .NET# Mono#Pla/orm# WP7# iOS# Android# Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  9. 9. What Code Can Be Shared?•Most non-UI or platform code•Core application logic•Entities•LINQ (objects, XML)•Network access (System.Net) Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  10. 10. Requirements•MonoTouch •Mac OS X •MonoDevelop•Mono for Android •Mac OS X or Windows •MonoDevelop or Visual Studio 2010•Windows Phone •Windows •Visual Studio 2010 Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  11. 11. File Linking•All profiles are not created equal•Single copy of file•Compile-time verification•Project Linker extension for VS2010 http://msdn.microsoft.com/en-us/library/ff648745 Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  12. 12. Abstraction•Separate common logic from UI•Interfaces, base classes, etc.public interface IContactManager{ IList<Person> GetContacts();}public class AndroidContactManager : IContactManager{ public IList<Person> GetContacts() { return null; }} Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  13. 13. Library: Xamarin.Mobile•Abstraction layer over platform APIs •Contacts •Geolocation •Camera •... http://xamarin.com/mobileapi Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  14. 14. Observer Pattern•Decouple UI from business logic•Business layer can publish updates•UI layer subscribes to updatespublic EventHandler<EventArgs> MessageReceived;MessageReceived += (sender, args) =>{ Console.WriteLine("Message received");}; Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  15. 15. Library: TinyMessenger•Event aggregator/messenger•Publish/subscribe•Single file•Supports iOS, Android, Windows Phone https://github.com/grumpydev/TinyMessenger Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  16. 16. Partial Classes and Methodspublic partial class MyClass{ // partial methods are private, and must return void partial void Foo(); public void Bar() { Foo(); }}public partial class MyClass{ partial void Foo() { Console.WriteLine("Foo"); }} Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  17. 17. Conditional Compilation#if __ANDROID__ Console.Write(“Only on Android”);#elif WINDOWS_PHONE Console.Write(“Only on Windows Phone”);#else Console.Write(“Everything else”);#endif Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  18. 18. Conditional Compilation•MonoTouch •No default symbols•Mono for Android •Windows Phone •__ANDROID__ •WINDOWS_PHONE •__ANDROID_1__ •SILVERLIGHT •__ANDROID_2__ •... Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  19. 19. File Access•Direct file access •System.IO •Works on iOS and Android •Not supported by Windows Phone •Paths are different on iOS and AndroidFile.WriteAllText( Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Writing directly to a file"); Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  20. 20. File Access•Isolated Storage •Works on iOS, Android, and Windows Phone •Higher level API •Don’t need to worry about file pathsusing (var store = IsolatedStorageFile.GetUserStoreForApplication())using (var writer = store.OpenFile("MyFile", FileMode.Create)){ // write to the file} Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  21. 21. Database Access•iOS and Android •SQLite •Native APIs or ADO.NET•Windows Phone •SQL Server CE •LINQ to SQL Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  22. 22. Library: C#-SQLite•C# port of SQLite database•File based•Useful for Windows Phone, Silverlight http://code.google.com/p/csharp-sqlite Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  23. 23. Library: sqlite-net•Layer over SQLite database•Strongly-typed queries•Single file•Supports iOS, Android, Windows Phone http://code.google.com/p/sqlite-net Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  24. 24. Library: TinyIoC•Inversion of Control container•Single file•Supports iOS, Android, Windows Phone https://github.com/grumpydev/TinyIoC Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  25. 25. Library: MonoCross•Cross-platform MVC framework•Shared models and controllers•Views specific to platforms•Supports iOS, Android, Windows Phone•Sits atop MonoTouch, Mono for Android http://code.google.com/p/monocross/ Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  26. 26. More LinksMWC 2012 Apphttps://github.com/xamarin/mobile-samples/tree/master/MWCNYC Code Camp 6 Apphttps://github.com/gshackles/NycCodeCamp6Mobile Development in C#http://amzn.com/1449320236 Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  27. 27. Xamarin Seminar Please give us your feedback http://bit.ly/xamfeedback Follow us on Twitter @XamarinHQ 19th April 2012 Copyright 2012 © Xamarin Inc. All rights reserved
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×