Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Cross platform mobile development in c#

1,795 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Cross platform mobile development in c#

  1. 1. Cross-Platform Mobile Development In C# By Dan Hermes Boston Mobile C# Developers' Group dan@lexiconsystemsinc.com www.lexiconsystemsinc.com
  2. 2. Going Native iOS  Android  Windows 
  3. 3. The Basics UI  Event life Cycles  Multi-Threading  Web Services  Patterns 
  4. 4. UI – Android  Xamarin Studio ◦ Xamarin Starter or Indie License  Visual Studio ◦ 2010+ ◦ Professional+ (for plug-ins) ◦ Xamarin Business or Enterprise License  .axml DEMO
  5. 5. UI - iOS Xcode  You need a Mac  ◦ Mac Mini ◦ macincloud.com DEMO
  6. 6. Xcode
  7. 7. Android: Activities  An Activity is like a… ◦ Windows Process ◦ ASP.NET Page Life Cycle ◦ MVC Page Life Cycle
  8. 8. Back vs. Home Image courtesy of Xamarin Back •User is done with the Activity. Destroy it. Home (or App Switcher) •Suspend the Activity. Place in the background.
  9. 9. Activity States Image courtesy of Xamarin
  10. 10. Activity Methods Image courtesy of Xamarin
  11. 11. OnCreate – WHEN? Creating views  Initializing variables  Binding static data to lists 
  12. 12. Bundle – for Activity State Key/value dictionary  Bundle not null in OnCreate? Restarting. 
  13. 13. Activity Methods  OnStart - before an activity becomes visible ◦ refresh current values of views  OnResume - start interacting with the user ◦ ◦ ◦ ◦ ◦ ◦  Ramping up frame rates Starting animations Listening for GPS updates Display any relevant alerts or dialogs Wire up external event handlers Undo operations in OnPause OnPause ◦ ◦ ◦ ◦ ◦ Commit changes to persistent data Destroy or clean up other objects consuming resources Ramp down frame rates and pausing animations Unregister external event handlers or notification handlers Clear dialogs and alerts
  14. 14. Activity Methods Image courtesy of Xamarin
  15. 15. Activity Methods  OnStop - activity is no longer visible to the user ◦ A new activity is being started and is covering up this activity. ◦ An existing activity is being brought to the foreground. ◦ The activity is being destroyed.  OnDestroy - final method ◦ Kill threads  OnRestart – prior to a restart ◦ Then OnStart – most reinit logic goes there
  16. 16. Managing State  Bundle ◦ Primitive data types  Custom Class ◦ Complex data (ex. Bitmap)  Roll Your Own ◦ Circumvent the configuration change lifecycle
  17. 17. iOS App Life Cycle Image courtesy of Xamarin
  18. 18. iOS Lifecycle Methods Image courtesy of Xamarin
  19. 19. iOS Lifecycle Methods  OnActivated ◦ upon launch or when app returns to foreground  OnResignActivation ◦ upon an interruption such as a text or phone call  DidEnterBackground ◦ user accepts the phone call ◦ save user data and tasks ◦ remove sensitive information from the screen  WillEnterForeground ◦ App returns to foreground from background or suspension ◦ Restore state saved during DidEnterBackground ◦ Then call OnActivated  WillTerminate ◦ App shuts down and process is destroyed ◦ If multitasking is not available on the device or the OS version ◦ Memory is low ◦ User manually terminates a backgrounded application.
  20. 20. iOS vs. Android App Life Cycle Image courtesy of Xamarin
  21. 21. iOS:Events, Protocols, and Delegates  Events ◦ Xamarin.iOS exposes .NET events on UIKit controls  Protocols ◦ Like a C# interface with optional methods ◦ Xamarin.iOS implements using abstract classes  Delegates ◦ strongly and weakly typed ◦ not be confused with C# delegates
  22. 22. Performance Mobile users expect it  Delays are annoying and alienate users  Android Activities and iOS Delegates?  ◦ run in the foreground UI Thread  Long-running code should go into a…
  23. 23. Background Thread
  24. 24. Good Candidates for a Background Thread  Operations that ◦ Make requests off of the device  Retrieve data (web service, RSS feed)  Connect to network  Downloads ◦ Run more than 50 milliseconds ◦ May block the user interface thread  Watchdog thread to kill the app
  25. 25. Async/Await async ◦ method declaration ◦ lambda ◦ anonymous method  await ◦ code stops and waits ◦ spawns background thread ◦ UI thread is not blocked ◦ resumes at the same point in the code 
  26. 26. Async/Await
  27. 27. Async/Await GetStringAsync  PutAsync  PostAsync  DeleteAsync 
  28. 28. Async Support  Xamarin.iOS ◦ 174 methods  Xamarin.Android ◦ 337 methods Xamarin.Mobile  Xamarin Component Store 
  29. 29. Web Services REST  WCF  SOAP 
  30. 30. REST      HttpWebRequest / WebClient RestSharp Hammock NSURLConnection ServiceStack
  31. 31. Web Patterns Async/Await  MVC  MVVMCross 
  32. 32. MVC  Model ◦ Can use them  Views ◦ Must use them  Controllers ◦ Android Activities ◦ iOS AppDelegates
  33. 33. Cross-Platform Mobile Development In C# Dan Hermes Mobile Consultant Lexicon Systems Are You Mobilizing? Website: www.lexiconsystemsinc.com Email: dan@lexiconsystemsinc.com Phone: 781-526-0738 Twitter: @lexiconsystems Blog: www.itshopkeeping.com

×