cross platform mobile apps using .net

64
Cross platform mobile apps using .NET Jonas Follesø NDC, June 2011

Upload: jonas-folleso

Post on 14-Dec-2014

6.846 views

Category:

Technology


1 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: Cross platform mobile apps using .NET

Cross platform mobile apps using .NET

Jonas Follesø

NDC, June 2011

Page 2: Cross platform mobile apps using .NET

http://www.flickr.com

/pho

tos/20792787@N00/2248623391/

AGENDA:

•Why?

•8 techniques

•Complete example

Page 3: Cross platform mobile apps using .NET
Page 4: Cross platform mobile apps using .NET
Page 5: Cross platform mobile apps using .NET

TILE FLOOD

Page 6: Cross platform mobile apps using .NET

TILE FLOOD

Page 7: Cross platform mobile apps using .NET
Page 8: Cross platform mobile apps using .NET

Why cross platform..?

Page 9: Cross platform mobile apps using .NET

REUSE SKILL SET?

Page 10: Cross platform mobile apps using .NET

private void open_Click(object sender, RoutedEventArgs e)

{ var channel = HttpNotificationChannel.Find("NNUG");

if (channel == null)

{

channel = new HttpNotificationChannel("NNUG");

}

channel.ChannelUriUpdated += channel_ChannelUriUpdated;

channel.ShellToastNotificationReceived +=

channel_ShellToastNotificationReceived;

channel.Open();

}

REUSE CODE?

Page 11: Cross platform mobile apps using .NET

REUSE LIBRARIES

• +35 libraries monotouch.info/MonoTouch/Libraries

Page 12: Cross platform mobile apps using .NET

COST OF MAINTAINING

MULTIPLE CODE BASES

Page 13: Cross platform mobile apps using .NET

LIKE C#..?

Page 14: Cross platform mobile apps using .NET

... OR DISLIKE THE ALTERNATIVE

Page 15: Cross platform mobile apps using .NET

OBJECTIVE-C: DATE (NO TIME)+ (NSDate *) stripTime:(NSDate *) date { NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *components = [gregorian components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date];

date = [gregorian dateFromComponents:components];

[gregorian release];

return date;}

Page 16: Cross platform mobile apps using .NET

C#: DATE (NO TIME)

public DateTime StripTime(DateTime date){ return date.Date;}

Page 17: Cross platform mobile apps using .NET
Page 18: Cross platform mobile apps using .NET

TECHNIQUE 1:

PORTABLE CLASS

LIBRARY

Page 19: Cross platform mobile apps using .NET

HELP YOU STICK TO A

COMMON SUBSET

Page 20: Cross platform mobile apps using .NET

PROS• One class library for all platforms• Helps you stick to a common

subset

CONS• No Preprocessor Directives• No Platform Specific Code in Class

Lib• Not directly supported on

MonoDevelop yet

PORTABLE CLASS LIBRARY

Page 21: Cross platform mobile apps using .NET

TECHNIQUE 2:

LINKED FILES

Page 22: Cross platform mobile apps using .NET

WORKS IN MONODEVEL

OP

Page 23: Cross platform mobile apps using .NET

PROS• Same code across all platforms• Ability to add platform specific

code• Preprocessor Directives

CONS• Need to manage multiple projects

LINKED FILES

Page 24: Cross platform mobile apps using .NET

TECHNIQUE 3:LINK FILES

USING «PROJECT

LINKER»

Page 25: Cross platform mobile apps using .NET

PROS• Auto link files between projects

CONS• Does not work in MonoDevelop• Must have all projects in same

solution

PROJECT LINKER

Page 26: Cross platform mobile apps using .NET

TECHNIQUE 4:VSMonoTouch

Open MonoTouch projects in

VS2010

Page 27: Cross platform mobile apps using .NET

PROS• Open and build (verify)

MonoTouch in VS2010

CONS• Not a «real» build• Some manual steps involved

VSMonoTouch

Page 28: Cross platform mobile apps using .NET

TECHNIQUE 5:PREPROCESSOR DIRECTIVES

Page 29: Cross platform mobile apps using .NET

using System.Net;#if MONOTOUCH || MONODROIDusing System.Web;#endif

Page 30: Cross platform mobile apps using .NET

PROS• Easily write platform specific code

CONS• Code smell• DRY – The #ifdefs tend to spread

PREPROCESSOR DIRECTIVES

Page 31: Cross platform mobile apps using .NET

demo

Page 32: Cross platform mobile apps using .NET

TECHNIQUE 6:ABSTRACT COMMON

FUNCTIONALITY

Page 33: Cross platform mobile apps using .NET

public interface IDispatchOnUIThread{ void Invoke(Action action);}

Page 34: Cross platform mobile apps using .NET

// Windows Phone 7public class DispatchAdapter : IDispatchOnUIThread{ public void Invoke(Action action) { Deployment.Current.Dispatcher.BeginInvoke(action); }}

Page 35: Cross platform mobile apps using .NET

// Mono for Androidpublic class DispatchAdapter : IDispatchOnUIThread{ private readonly Activity _owner;

public DispatchAdapter(Activity owner) { _owner = owner; }

public void Invoke(Action action) { _owner.RunOnUiThread(action); }}

Page 36: Cross platform mobile apps using .NET

// Monotouchpublic class DispatchAdapter : IDispatchOnUIThread{

private readonly NSObject _owner;

public DispatchAdapter(NSObject owner){

_owner = owner;}

public void Invoke (Action action){

_owner.BeginInvokeOnMainThread(new NSAction(action));

}}

Page 37: Cross platform mobile apps using .NET

PROS• Share even more code• Do not need preprocessor

directives

CONS• More abstractions in code

ABSTRACT FUNCTIONALITY

Page 38: Cross platform mobile apps using .NET

TECHNIQUE 7:LEVERAGE

MVVM ON ALL PLATFORMS

Page 39: Cross platform mobile apps using .NET

39

Ensure that any code that manipulates presentation only manipulates presentation, pushing all domain and data source logic into clearly separated areas of the program.

Martin Fowler, Separated Presentation, July 2006

Page 40: Cross platform mobile apps using .NET
Page 41: Cross platform mobile apps using .NET

41Separated Presentation

Patterns

Page 42: Cross platform mobile apps using .NET

42

Data & Domain Logic(Model)

UI(View)

Interaction (Controller/Presenter)

Page 43: Cross platform mobile apps using .NET

MVVM

PATTERN FOR

BINDABLE UI

Page 44: Cross platform mobile apps using .NET

Data Model

VIEW

XAML

Code-BehindEvent

Handlers

APP LOGIC IN CODE BEHIND IS HARD TO

TEST AND MAINTAIN

Page 45: Cross platform mobile apps using .NET

Data Model

VIEW

XAML

Code-Behind

VIEW MODEL

State + Operations

Change notification

Data-binding and commands

SEPARATE USING MVVM

Page 46: Cross platform mobile apps using .NET

public class ObservableAdapter<T> : BaseAdapter<T>{ private readonly Activity _context; private readonly ObservableCollection<T> _collection;

public ObservableAdapter(Activity context, ObservableCollection<T>

collection) { _context = context; _collection = collection; _collection.CollectionChanged += (o, e) =>

NotifyDataSetChanged(); }...

ListAdapter = new ObservableAdapter<Airport>(this, _viewModel.Airports);

Page 47: Cross platform mobile apps using .NET

public abstract class ObservableDataSource<T> : UITableViewDataSource{

public ObservableDataSource (ObservableCollection<T> collection, UITableView

tableView){

_tableView = tableView;_collection = collection;_collection.CollectionChanged += (o, e) => {

tableView.ReloadData();};

}...

Page 48: Cross platform mobile apps using .NET

PROS• Share even more code• Testable View Models

CONS• More abstractions in code• May not feel natural to platform

APPLYING MVVM

Page 49: Cross platform mobile apps using .NET

demo

Page 50: Cross platform mobile apps using .NET

TECHNIQUE 8:USE PRE-

BUILT ABSTRACTION

S

Page 51: Cross platform mobile apps using .NET

PHONE SPECIFIC

APIS?

htt

p:/

/ww

w.fl

ickr

.com

/ph

oto

s/h

ow

zey/2

88

04

55

76

2/

Page 52: Cross platform mobile apps using .NET

EXAMPLES

• Accelerometer• Camera• Compass• Geolocation• Notification• Storage• Contacts• And more...

Page 53: Cross platform mobile apps using .NET

MonoMobile.Extensions

http://www.flickr.com

/pho

tos/tambako/3974809361/

• https://github.com/chrisntr/MonoMobile.Extensions

• One common API for phone specific functionality across all three devices

• Inspired by PhoneGap(same API)

Page 54: Cross platform mobile apps using .NET

private void FindNearestAirport(){ var coordinates = new MonoMobile.Extensions.Geolocation(); coordinates.GetCurrentPosition(PositionAvailable);}

private void PositionAvailable(Position position){ Console.WriteLine("{0} {1}", position.Coords.Latitude, position.Coords.Longitude);}

Page 55: Cross platform mobile apps using .NET

private void FindNearestAirport(){ var coordinates = new MonoMobile.Extensions.Geolocation(); coordinates.GetCurrentPosition(PositionAvailable);}

private void PositionAvailable(Position position){ Console.WriteLine("{0} {1}", position.Coords.Latitude, position.Coords.Longitude);}

Page 56: Cross platform mobile apps using .NET

3x MonoMobile.Extensions.dll• MonoDroid -> MonoMobile.Extensions.dll• Implemented using LocationManager

• MonoTouch -> MonoMobile.Extensions.dll• Implemented using CLLocationManager

• Windows Phone 7 -> MonoMobile.Extensions.dll• Implemented using

GeoLocationWatcher

Page 57: Cross platform mobile apps using .NET

PROS• Share even more code• Same set of abstractions used in

multiple apps• Well known API

CONS• More abstractions in code• Smallest common denominator

ABSTRACT FUNCTIONALITY

Page 58: Cross platform mobile apps using .NET

DOES IT WORK IN

PRACTICE?

http://www.flickr.com

/pho

tos/kozloski/2306510520/

Page 59: Cross platform mobile apps using .NET

FLIGHTS NORWAY

MONITORARRIVALS

AND DEPARTURES

http://www.flickr.com

/pho

tos/svenwerk/2181849280/

Page 60: Cross platform mobile apps using .NET
Page 61: Cross platform mobile apps using .NET

demo

Page 62: Cross platform mobile apps using .NET

1. Portable Library2. Linked Files3. Project Linker4. VSMonoTouch5. Preprocessor Directives6. Abstracting Common

Functionality7. Leverage MVVM8. Using Pre-built Abstractions

SUMMARY

Page 63: Cross platform mobile apps using .NET

FORK ME ON

GITHUBgithub.com/follesoe/FlightsNorway

• Complete app for iOS, Android & WP7

• 56 page tutorial with step-by-step code.

Page 64: Cross platform mobile apps using .NET

BEKK CONSULTING ASSKUR 39, VIPPETANGEN. P.O. BOX 134 SENTRUM, 0102 OSLO, NORWAY.

WWW.BEKK.NO

Jonas FollesøScientist

+47 977 [email protected]

[email protected]