tips & tricks for sharing c# code on ios, android and windows phone by jaime rodriguez

46
Building Windows phone, iOS and Android apps with C# Jaime Rodriguez Principal Evangelist, Microsoft

Upload: net-conf-uy

Post on 07-Jul-2015

256 views

Category:

Technology


0 download

DESCRIPTION

Tips & tricks for sharing C# code on iOS, Android and Windows Phone Jaime Rodriguez .NET Conf UY 2014 http://netconf.uy

TRANSCRIPT

Page 1: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

Building Windows phone, iOS

and Android apps with C#

Jaime Rodriguez

Principal Evangelist, Microsoft

Page 2: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

About me Why this talk….

90s 2000 2005 2008-Today

Primitives Productivity UX Mobile

Disclaimer

Page 3: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

About me Why this talk….

90s 2000 2005 2008-Today

Primitives Productivity UX Mobile

Disclaimer

I do not work for Xamarin

Opinions are my own, not those of my day-job employer

This space evolves very fast

Page 4: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

Mobile Explosion

Consumers are already mobile-first

Business users are increasingly demanding mobile scenarios

Page 5: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

How? #1 – Web

Build a Mobile

Website

Page 6: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

How? #2 – Hybrid Web

Put a Web App

In the Store

Native App

Mobile

Website

Page 7: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

How? #3 – Cloned Native

Build App

Multiple Times

Page 8: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

How? #4 – Shared Native

Shared UI Code

Build Natively

and Share Code

Page 9: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

Why Native?

Xamarin apps look and feel native because they are native

Native User Interfaces Native API Access Native Performance

Page 10: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

Start with C# and BCL

Page 11: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

… add Windows APIs

100% coverage

Page 12: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

… or iOS APIs

100% coverage

Page 13: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

… or Android APIs

100% coverage

Page 14: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

demoXamarin Development with Visual Studio

Page 15: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

@implementation MSViewController

- (void)viewDidLoad

{

[super viewDidLoad];

}

- (IBAction)OnButtonDown:(id)sender {

UIAlertView* view =

[[UIAlertView alloc]init];

[view setTitle:@"Hello World"];

[view setMessage: @"How are you?”];

[view addButtonWithTitle:@"OK"];

[view show];

}

@end

public partial class iOSAppViewController : UIViewController

{

public iOSAppViewController (IntPtr handle) : base (handle){

}

public override void ViewDidLoad (){base.ViewDidLoad ();

}

partial void OnButtonDown (UIButton sender){

UIAlertView view = new UIAlertView(); view.Title = "Hello World" ; view.Message = "How are you? " ; view.AddButton ("OK"); view.Show();

}

}

iOS

Page 16: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

public class MyActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

Button myBtn = (Button) this.findViewById( R.id.clickMe);

myBtn.setOnClickListener( new View.OnClickListener() {

@Override

public void onClick(View view) {

AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this) ;

builder.setTitle( "Hello World")

.setMessage("How are you?")

.setPositiveButton( "OK", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterfacedialogInterface, int i) {

dialogInterface.dismiss();

}}) .show();

}});}

}

[Activity (Label = "AndroidApp", MainLauncher = true, Icon = "@drawable/icon")]

public class MainActivity : Activity {protected override void OnCreate (Bundle bundle){

base.OnCreate (bundle);SetContentView (Resource.Layout.Main);Button button = FindViewById<Button> (Resource.Id.me);

button.Click += delegate {AlertDialog.Builder builder = new AlertDialog.Builder

(this ); AlertDialog dialog = null ; builder.SetTitle ( "Hello World")

.SetMessage ( "How are you")

.SetPositiveButton( "OK" , delegate { dialog.Dismiss(); } );

dialog = builder.Show ();

} ;

}}

Android

Page 17: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

Anything you can do in Objective-C or Java can be done in C# with Xamarin using Visual Studio

Page 18: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

Native Performance

Xamarin.iOS does full Ahead Of Time

(AOT) compilation to produce an ARM

binary for Apple’s App Store.

Xamarin.Android takes advantage of

Just In Time (JIT) compilation on the

Android device.

Page 19: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

So far…

Using C# BCL on iOS, Android and Windows Phone apps

C# Bindings to iOS/Android

Great tooling Editors

Debugging

Extensibility

Potentially cumbersome code sharing as you write platform specific code ?

Page 20: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

Sharing v1Really?

Page 21: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

Linked

Files

Compiler

Directives

Page 22: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

Code sharing v3

Page 23: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

Share Code: Portable Class Libraries

Page 24: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

NuGet

Page 25: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

Shared Projects

Page 26: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

UI: Xamarin + Xamarin.Forms

With Xamarin.Forms:

more code-sharing, native controlsTraditional Xamarin approach

Shared UI Code

Page 27: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

40+ Pages, Layouts, and Controls

Build from code behind or XAML

Two-way Data Binding

Navigation

Animation API

Dependency Service

Messaging Center

UI: Xamarin.Forms

Shared UI Code

Page 28: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

Layouts

Stack Absolute Relative Grid ContentView ScrollView Frame

Page 29: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

ControlsActivityIndicator BoxView Button DatePicker Editor

Entry Image Label ListView Map

OpenGLView Picker ProgressBar SearchBar Slider

Stepper TableView TimePicker WebView EntryCell

ImageCell SwitchCell TextCell ViewCell

Page 30: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

Xamarin Forms

Mark-up (XAML 2009 spec)

Data binding & Data Templates

Markup Extensions

Resources Dictionaries

Page 31: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

Xamarin Forms Platform Features

Page.DisplayAlert

UI Thread marshalling

Timers

Xamarin.Forms.Maps

Platform code via OnPlatform<T> and DependencyService.Get<T>

Page 32: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

demoHacking away with Xamarin and Visual Studio

Page 33: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

Tips & Tricks Personal Observations & Lessons learned

Page 34: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

Ramp-up & Mastery

To be successful using C# on iOS, Android, and Windows Phone, you still have to know how to code for these platforms

Page 35: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

How I learned native…

Page 36: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

How I learned Xamarin…

Page 37: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

Sharing code… what should you use?

a) PCL

b) Shared Projects

c) partial classes

d) C# extensions

e) All of the above

Page 38: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

UI Patterns & Tips

Separate your concerns

Declarative XAML

MVVM is not required

Use OnPlatform<T> for platform specific code

Use ContentPage + layout panels for dynamic resolution

Page 39: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

Connect to the cloud: Microsoft Azure

http://azure.microsoft.com/en-us/documentation/services/mobile-services/

Page 40: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

Memory & Garbage Collection

iOS

Uses AOT (Ahead of Time) compiler

Two GCs: default (Boehm) or Sgen

Android

Uses Sgen GC

Windows Phone

Uses .NET GC,

http://developer.xamarin.com/guides/cross-platform/application_fundamentals/memory_perf_best_practices/

http://msdn.microsoft.com/en-us/library/ms973837.aspx

Page 41: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

Performance

Is usually not a problem

Use native tools to measure iOS: Instruments Android: Device Monitor ’s Allocation ManagerWindows Phone: Visual Studio, Windows Phone Power tools,

Graphics Diagnostics

Page 42: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

For gamers

Page 43: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

Closing: Mobile app development with C#

Familiar

Productive

Highly reusable

Empowering

Built on a solid and extensible foundation

Page 44: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

http://xamarin.com/MSDN

Page 45: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

Want to win a free Xamarin license?

1. Download free version of Xamarin Studio …

2. Create a small Hello World Project on at least two of the three platforms: Windows Phone, Android, iOS.

3. Tweet a link to your project to @jaimerodriguez before October 10th at noon PST

4. One random submission will be selected….and a coupon for free license will be emailed

Page 46: Tips & tricks for sharing C# code on iOS, Android and Windows Phone by Jaime Rodriguez

Gracias!!!

Aqui estoy los tres dias!

@jaimerodriguez

[email protected]