Xamarin
   Seminar
       19th April 2012
    Copyright 2012 © Xamarin Inc. All rights reserved
Agenda
Cross-Platform Mobile Development

                            Greg Shackles
                            Senior Software Engineer
                            OLO Online Ordering
                            greg@gregshackles.com

                            gregshackles.com
                            @gshackles
                            github.com/gshackles
                                                             Xamarin
         Copyright 2012 © Xamarin Inc. All rights reserved
Introduction
This session will discuss how to use C# to develop
iOS, Android and Windows Phone applications.


We’ll cover:

 •Why use C#?
 •Code sharing techniques and patterns
 •Useful libraries

                                                                     Xamarin
                 Copyright 2012 © Xamarin Inc. All rights reserved
Native Platform Languages




                                                            Xamarin
        Copyright 2012 © Xamarin Inc. All rights reserved
Write Once, Run Anywhere?




    !=                                                       !=



                                                                  Xamarin
         Copyright 2012 © Xamarin Inc. All rights reserved
C# to the Rescue!




                                                        Xamarin
    Copyright 2012 © Xamarin Inc. All rights reserved
Benefits

•C# and .NET are mature and powerful
•Skill reuse on all platforms
•Apps are still completely native
•Code can be reused across platforms
 (even non-mobile platforms!)

                                                               Xamarin
           Copyright 2012 © Xamarin Inc. All rights reserved
Architecture
                                                                         Mono#for#
  UI#      Silverlight#                   MonoTouch#
                                                                         Android#


  C##                                   Business#Logic#



Run)me#       .NET#                                              Mono#



Pla/orm#      WP7#                                iOS#                   Android#




                                                                                     Xamarin
             Copyright 2012 © Xamarin Inc. All rights reserved
What Code Can Be Shared?

•Most non-UI or platform code
•Core application logic
•Entities
•LINQ (objects, XML)
•Network access (System.Net)
                                                               Xamarin
           Copyright 2012 © Xamarin Inc. All rights reserved
Requirements
•MonoTouch
  •Mac OS X
  •MonoDevelop
•Mono for Android
  •Mac OS X or Windows
  •MonoDevelop or Visual Studio 2010
•Windows Phone
  •Windows
  •Visual Studio 2010

                                                                            Xamarin
                        Copyright 2012 © Xamarin Inc. All rights reserved
File Linking

•All profiles are not created equal
•Single copy of file
•Compile-time verification
•Project Linker extension for VS2010
 http://msdn.microsoft.com/en-us/library/ff648745


                                                                  Xamarin
              Copyright 2012 © Xamarin Inc. All rights reserved
Abstraction

•Separate common logic from UI
•Interfaces, base classes, etc.
public interface IContactManager
{
  IList<Person> GetContacts();
}

public class AndroidContactManager : IContactManager
{
    public IList<Person> GetContacts()
    {
  
   return null;
    }
}                                                                         Xamarin
                      Copyright 2012 © Xamarin Inc. All rights reserved
Library: Xamarin.Mobile

•Abstraction layer over platform APIs
  •Contacts
  •Geolocation
  •Camera
  •...
          http://xamarin.com/mobileapi


                                                                     Xamarin
                 Copyright 2012 © Xamarin Inc. All rights reserved
Observer Pattern

•Decouple UI from business logic
•Business layer can publish updates
•UI layer subscribes to updates
public EventHandler<EventArgs> MessageReceived;

MessageReceived += (sender, args) =>
{
   Console.WriteLine("Message received");
};

                                                                           Xamarin
                       Copyright 2012 © Xamarin Inc. All rights reserved
Library: TinyMessenger

•Event aggregator/messenger
•Publish/subscribe
•Single file
•Supports iOS, Android, Windows Phone
   https://github.com/grumpydev/TinyMessenger


                                                                  Xamarin
              Copyright 2012 © Xamarin Inc. All rights reserved
Partial Classes and
                  Methods
public partial class MyClass
{
   // partial methods are private, and must return void
  partial void Foo();

    public void Bar()
    {
      Foo();
    }
}

public partial class MyClass
{
  partial void Foo()
  {
     Console.WriteLine("Foo");
  }
}

                                                                            Xamarin
                        Copyright 2012 © Xamarin Inc. All rights reserved
Conditional Compilation

#if __ANDROID__
   Console.Write(“Only on Android”);
#elif WINDOWS_PHONE
   Console.Write(“Only on Windows Phone”);
#else
   Console.Write(“Everything else”);
#endif



                                                                  Xamarin
              Copyright 2012 © Xamarin Inc. All rights reserved
Conditional Compilation
•MonoTouch
  •No default symbols
•Mono for Android                              •Windows Phone
  •__ANDROID__                                         •WINDOWS_PHONE
  •__ANDROID_1__                                       •SILVERLIGHT
  •__ANDROID_2__
  •...

                                                                    Xamarin
                Copyright 2012 © Xamarin Inc. All rights reserved
File Access
•Direct file access
    •System.IO
    •Works on iOS and Android
    •Not supported by Windows Phone
    •Paths are different on iOS and Android
File.WriteAllText(
 Environment.GetFolderPath(Environment.SpecialFolder.Personal),
 "Writing directly to a file");

                                                                         Xamarin
                     Copyright 2012 © Xamarin Inc. All rights reserved
File Access
•Isolated Storage
    •Works on iOS, Android, and Windows Phone
    •Higher level API
    •Don’t need to worry about file paths

using (var store = IsolatedStorageFile.GetUserStoreForApplication())
using (var writer = store.OpenFile("MyFile", FileMode.Create))
{
  // write to the file
}

                                                                             Xamarin
                         Copyright 2012 © Xamarin Inc. All rights reserved
Database Access

•iOS and Android
  •SQLite
  •Native APIs or ADO.NET
•Windows Phone
  •SQL Server CE
  •LINQ to SQL
                                                                  Xamarin
              Copyright 2012 © Xamarin Inc. All rights reserved
Library: C#-SQLite


•C# port of SQLite database
•File based
•Useful for Windows Phone, Silverlight
     http://code.google.com/p/csharp-sqlite



                                                                 Xamarin
             Copyright 2012 © Xamarin Inc. All rights reserved
Library: sqlite-net

•Layer over SQLite database
•Strongly-typed queries
•Single file
•Supports iOS, Android, Windows Phone
      http://code.google.com/p/sqlite-net


                                                                 Xamarin
             Copyright 2012 © Xamarin Inc. All rights reserved
Library: TinyIoC


•Inversion of Control container
•Single file
•Supports iOS, Android, Windows Phone
     https://github.com/grumpydev/TinyIoC



                                                                 Xamarin
             Copyright 2012 © Xamarin Inc. All rights reserved
Library: MonoCross

•Cross-platform MVC framework
•Shared models and controllers
•Views specific to platforms
•Supports iOS, Android, Windows Phone
•Sits atop MonoTouch, Mono for Android
      http://code.google.com/p/monocross/
                                                                 Xamarin
             Copyright 2012 © Xamarin Inc. All rights reserved
More Links

MWC 2012 App
https://github.com/xamarin/mobile-samples/tree/master/MWC


NYC Code Camp 6 App
https://github.com/gshackles/NycCodeCamp6


Mobile Development in C#
http://amzn.com/1449320236


                                                                      Xamarin
                  Copyright 2012 © Xamarin Inc. All rights reserved
Xamarin
    Seminar
   Please give us your feedback
  http://bit.ly/xamfeedback


      Follow us on Twitter
        @XamarinHQ

           19th April 2012
        Copyright 2012 © Xamarin Inc. All rights reserved

Cross-platform Mobile Development

  • 1.
    Xamarin Seminar 19th April 2012 Copyright 2012 © Xamarin Inc. All rights reserved
  • 2.
    Agenda Cross-Platform Mobile Development Greg Shackles Senior Software Engineer OLO Online Ordering greg@gregshackles.com gregshackles.com @gshackles github.com/gshackles Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 3.
    Introduction This session willdiscuss how to use C# to develop iOS, Android and Windows Phone applications. We’ll cover: •Why use C#? •Code sharing techniques and patterns •Useful libraries Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 4.
    Native Platform Languages Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 5.
    Write Once, RunAnywhere? != != Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 6.
    C# to theRescue! Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 7.
    Benefits •C# and .NETare mature and powerful •Skill reuse on all platforms •Apps are still completely native •Code can be reused across platforms (even non-mobile platforms!) Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 8.
    Architecture Mono#for# UI# Silverlight# MonoTouch# Android# C## Business#Logic# Run)me# .NET# Mono# Pla/orm# WP7# iOS# Android# Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 9.
    What Code CanBe Shared? •Most non-UI or platform code •Core application logic •Entities •LINQ (objects, XML) •Network access (System.Net) Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 10.
    Requirements •MonoTouch •MacOS X •MonoDevelop •Mono for Android •Mac OS X or Windows •MonoDevelop or Visual Studio 2010 •Windows Phone •Windows •Visual Studio 2010 Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 11.
    File Linking •All profilesare not created equal •Single copy of file •Compile-time verification •Project Linker extension for VS2010 http://msdn.microsoft.com/en-us/library/ff648745 Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 12.
    Abstraction •Separate common logicfrom UI •Interfaces, base classes, etc. public interface IContactManager { IList<Person> GetContacts(); } public class AndroidContactManager : IContactManager { public IList<Person> GetContacts() { return null; } } Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 13.
    Library: Xamarin.Mobile •Abstraction layerover platform APIs •Contacts •Geolocation •Camera •... http://xamarin.com/mobileapi Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 14.
    Observer Pattern •Decouple UIfrom business logic •Business layer can publish updates •UI layer subscribes to updates public EventHandler<EventArgs> MessageReceived; MessageReceived += (sender, args) => { Console.WriteLine("Message received"); }; Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 15.
    Library: TinyMessenger •Event aggregator/messenger •Publish/subscribe •Singlefile •Supports iOS, Android, Windows Phone https://github.com/grumpydev/TinyMessenger Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 16.
    Partial Classes and Methods public partial class MyClass { // partial methods are private, and must return void partial void Foo(); public void Bar() { Foo(); } } public partial class MyClass { partial void Foo() { Console.WriteLine("Foo"); } } Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 17.
    Conditional Compilation #if __ANDROID__ Console.Write(“Only on Android”); #elif WINDOWS_PHONE Console.Write(“Only on Windows Phone”); #else Console.Write(“Everything else”); #endif Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 18.
    Conditional Compilation •MonoTouch •No default symbols •Mono for Android •Windows Phone •__ANDROID__ •WINDOWS_PHONE •__ANDROID_1__ •SILVERLIGHT •__ANDROID_2__ •... Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 19.
    File Access •Direct fileaccess •System.IO •Works on iOS and Android •Not supported by Windows Phone •Paths are different on iOS and Android File.WriteAllText( Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Writing directly to a file"); Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 20.
    File Access •Isolated Storage •Works on iOS, Android, and Windows Phone •Higher level API •Don’t need to worry about file paths using (var store = IsolatedStorageFile.GetUserStoreForApplication()) using (var writer = store.OpenFile("MyFile", FileMode.Create)) { // write to the file } Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 21.
    Database Access •iOS andAndroid •SQLite •Native APIs or ADO.NET •Windows Phone •SQL Server CE •LINQ to SQL Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 22.
    Library: C#-SQLite •C# portof SQLite database •File based •Useful for Windows Phone, Silverlight http://code.google.com/p/csharp-sqlite Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 23.
    Library: sqlite-net •Layer overSQLite database •Strongly-typed queries •Single file •Supports iOS, Android, Windows Phone http://code.google.com/p/sqlite-net Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 24.
    Library: TinyIoC •Inversion ofControl container •Single file •Supports iOS, Android, Windows Phone https://github.com/grumpydev/TinyIoC Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 25.
    Library: MonoCross •Cross-platform MVCframework •Shared models and controllers •Views specific to platforms •Supports iOS, Android, Windows Phone •Sits atop MonoTouch, Mono for Android http://code.google.com/p/monocross/ Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 26.
    More Links MWC 2012App https://github.com/xamarin/mobile-samples/tree/master/MWC NYC Code Camp 6 App https://github.com/gshackles/NycCodeCamp6 Mobile Development in C# http://amzn.com/1449320236 Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 27.
    Xamarin Seminar Please give us your feedback http://bit.ly/xamfeedback Follow us on Twitter @XamarinHQ 19th April 2012 Copyright 2012 © Xamarin Inc. All rights reserved