Cross-Platform Mobile Development In C#By Dan Hermes
Boston Mobile C# Developers' Group
Going NativeiOSAndroidWindows
The BasicsUIEvent life CyclesMulti-ThreadingWeb ServicesPatterns
UI – AndroidXamarin Studio
◦Xamarin Starter or Indie LicenseVisual Studio
◦2010+◦Professional+ (for plug-ins)◦Xamarin Business or Enterprise
License.axml
DEMO
UI - iOSXcodeYou need a Mac
◦Mac Mini◦macincloud.com
DEMO
Xcode
Android: Activities
An Activity is like a…◦Windows Process◦ASP.NET Page Life Cycle◦MVC Page Life Cycle
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.
Activity States
Image courtesy of Xamarin
Activity Methods
Image courtesy of Xamarin
OnCreate – WHEN?Creating viewsInitializing variablesBinding static data to lists
Bundle – for Activity StateKey/value dictionaryBundle not null in OnCreate?
Restarting.
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
Activity Methods
Image courtesy of Xamarin
Activity MethodsOnStop - 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
Managing StateBundle
◦Primitive data typesCustom Class
◦Complex data (ex. Bitmap)Roll Your Own
◦Circumvent the configuration change lifecycle
iOS App Life Cycle
Image courtesy of Xamarin
iOS Lifecycle Methods
Image courtesy of Xamarin
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.
iOS vs. AndroidApp Life Cycle
Image courtesy of Xamarin
iOS:Events, Protocols, and DelegatesEvents
◦Xamarin.iOS exposes .NET events on UIKit controls
Protocols ◦ Like a C# interface with optional methods◦ Xamarin.iOS implements using abstract
classesDelegates
◦ strongly and weakly typed◦ not be confused with C# delegates
Performance
Mobile users expect itDelays are annoying and alienate
usersAndroid Activities and iOS
Delegates?◦run in the foreground UI Thread
Long-running code should go into a…
Background Thread
Good Candidates for a Background ThreadOperations 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
Async/Awaitasync
◦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
Async/Await
Async/AwaitGetStringAsync PutAsync PostAsync DeleteAsync
Async SupportXamarin.iOS
◦174 methodsXamarin.Android
◦337 methodsXamarin.MobileXamarin Component Store
Web ServicesRESTWCFSOAP
REST HttpWebRequest / WebClient RestSharp Hammock NSURLConnection ServiceStack
Web PatternsAsync/AwaitMVCMVVMCross
MVCModel
◦Can use themViews
◦Must use themControllers
◦Android Activities◦iOS AppDelegates
Cross-Platform Mobile Development In C#Dan HermesMobile Consultant
Lexicon Systems
Website: www.lexiconsystemsinc.comEmail: [email protected]: 781-526-0738Twitter: @lexiconsystemsBlog: www.itshopkeeping.com
Are You Mobilizing?
Top Related