33
Cross-Platform Mobile Development In C# By Dan Hermes [email protected] www.lexiconsystemsinc.com Boston Mobile C# Developers' Group

Cross platform mobile development in c#

Embed Size (px)

Citation preview

Page 1: Cross platform mobile development in c#

Cross-Platform Mobile Development In C#By Dan Hermes

[email protected]

Boston Mobile C# Developers' Group

Page 2: Cross platform mobile development in c#

Going NativeiOSAndroidWindows

Page 3: Cross platform mobile development in c#

The BasicsUIEvent life CyclesMulti-ThreadingWeb ServicesPatterns

Page 4: Cross platform mobile development in c#

UI – AndroidXamarin Studio

◦Xamarin Starter or Indie LicenseVisual Studio

◦2010+◦Professional+ (for plug-ins)◦Xamarin Business or Enterprise

License.axml

DEMO

Page 5: Cross platform mobile development in c#

UI - iOSXcodeYou need a Mac

◦Mac Mini◦macincloud.com

DEMO

Page 6: Cross platform mobile development in c#

Xcode

Page 7: Cross platform mobile development in c#

Android: Activities

An Activity is like a…◦Windows Process◦ASP.NET Page Life Cycle◦MVC Page Life Cycle

Page 8: Cross platform mobile development in c#

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.

Page 9: Cross platform mobile development in c#

Activity States

Image courtesy of Xamarin

Page 10: Cross platform mobile development in c#

Activity Methods

Image courtesy of Xamarin

Page 11: Cross platform mobile development in c#

OnCreate – WHEN?Creating viewsInitializing variablesBinding static data to lists

Page 12: Cross platform mobile development in c#

Bundle – for Activity StateKey/value dictionaryBundle not null in OnCreate?

Restarting.

Page 13: Cross platform mobile development in c#

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

Page 14: Cross platform mobile development in c#

Activity Methods

Image courtesy of Xamarin

Page 15: Cross platform mobile development in c#

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

Page 16: Cross platform mobile development in c#

Managing StateBundle

◦Primitive data typesCustom Class

◦Complex data (ex. Bitmap)Roll Your Own

◦Circumvent the configuration change lifecycle

Page 17: Cross platform mobile development in c#

iOS App Life Cycle

Image courtesy of Xamarin

Page 18: Cross platform mobile development in c#

iOS Lifecycle Methods

Image courtesy of Xamarin

Page 19: Cross platform mobile development in c#

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.

Page 20: Cross platform mobile development in c#

iOS vs. AndroidApp Life Cycle

Image courtesy of Xamarin

Page 21: Cross platform mobile development in c#

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

Page 22: Cross platform mobile development in c#

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…

Page 23: Cross platform mobile development in c#

Background Thread

Page 24: Cross platform mobile development in c#

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

Page 25: Cross platform mobile development in c#

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

Page 26: Cross platform mobile development in c#

Async/Await

Page 27: Cross platform mobile development in c#

Async/AwaitGetStringAsync PutAsync PostAsync DeleteAsync

Page 28: Cross platform mobile development in c#

Async SupportXamarin.iOS

◦174 methodsXamarin.Android

◦337 methodsXamarin.MobileXamarin Component Store

Page 29: Cross platform mobile development in c#

Web ServicesRESTWCFSOAP

Page 30: Cross platform mobile development in c#

REST HttpWebRequest / WebClient RestSharp Hammock NSURLConnection ServiceStack

Page 31: Cross platform mobile development in c#

Web PatternsAsync/AwaitMVCMVVMCross

Page 32: Cross platform mobile development in c#

MVCModel

◦Can use themViews

◦Must use themControllers

◦Android Activities◦iOS AppDelegates

Page 33: Cross platform mobile development in c#

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?