xamarin.mac seminar

44
Xamarin.Mac Introduc0on Miguel de Icaza, CTO Xamarin [email protected] January 10 th 2013

Upload: xamarin

Post on 13-Jan-2015

16.612 views

Category:

Technology


2 download

DESCRIPTION

Learn to write native Mac apps using C# and .NET directly from Miguel de Icaza, cofounder and CTO of Xamarin.

TRANSCRIPT

Page 1: Xamarin.Mac Seminar

Xamarin.Mac  Introduc0on  

Miguel  de  Icaza,  CTO  Xamarin  [email protected]  January  10th  2013  

Page 2: Xamarin.Mac Seminar

Xamarin.Mac  Overview  

•  Build  Mac  applica0ons  with  C#  – Use  C#  and  .NET  libraries  on  Mac  – All  that  you  love:    

•  LINQ,  Task  Parallel  Library,  async  •  GC,  type  safety,  generics,  dynamic  and  more.  

– Mac  AppStore  ready  

•  Na0ve  Applica0ons  – Use  na0ve  APIs  to  deeply  integrate  with  OSX  

Page 3: Xamarin.Mac Seminar

Xamarin.Mac Overview

2.2B devices reachable with C#

Page 4: Xamarin.Mac Seminar

Xamarin.Mac  at  a  Glance  

Mono  Run0me  

Xamaric.Mac  Libraries  

Mono  Core  Class  Libraries  

Tools  and  SDK    

•  Binder  •  Bundler  •  Linker  •  Packager  

 

MonoDevelop  IDE   Xcode  (UI  Designer)  

Cocoa  Frameworks  

System  Libraries  

Darwin  OS  

Page 5: Xamarin.Mac Seminar

Xamarin.Mac  Frameworks  

•  CoreGraphics  •  CoreImage  •  CoreText  •  CoreVideo  

Graphics  •  ImageKit  •  ImageIO  •  OpenGL  •  PDFKit  

•  AppKit  •  CoreAnima0on  •  CoreImage  

User  Interface  •  QCComposer  •  QuickLook  •  SceneKit  •  WebKit  

•  AVFounda0on  •  AudioToolbox  •  AudioUnit  

Audio  and  Video  •  CoreMidi  •  CoreMedia  •  OpenAL  

•  AddressBook  •  Bluetooth  •  CoreLoca0on  •  CoreServices  

System  Services  •  CoreWLan  •  Scrip0ngBridge  •  StoreKit  

•  CoreData  •  CoreFounda0on  •  Darwin  

Infrastructure  

•  Founda0on  •  ObjCRun0me  •  Security  

Page 6: Xamarin.Mac Seminar

Lots  shared  with  MonoTouch  (iOS)  

•  CoreGraphics  •  CoreImage  •  CoreText  •  CoreVideo  

Graphics  •  ImageKit  •  ImageIO  •  OpenGL  •  PDFKit  

•  AppKit  •  CoreAnima0on  •  CoreGraphics  •  CoreImage  

User  Interface  •  QCComposer  •  QuickLook  •  SceneKit  •  WebKit  

•  AVFounda0on  •  AudioToolbox  •  AudioUnit  

Audio  and  Video  •  CoreMidi  •  CoreMedia  •  OpenAL  

•  AddressBook  •  Bluetooth  •  CoreLoca0on  •  CoreServices  

System  Services  •  CoreWLan  •  Scrip0ngBridge  •  StoreKit  

•  CoreData  •  CoreFounda0on  •  Darwin  

Infrastructure  

•  Founda0on  •  ObjCRun0me  •  Security  

Page 7: Xamarin.Mac Seminar

How  does  Xamarin.Mac  work?  •  OSX  Libraries  Projected  to  C#  

–  1:1  mapping  from  OSX  na0ve  APIs  to  C#  

•  Objec0ve-­‐C  (80%  of  APIs)  – Object  system  mapped  –  Supports  subclassing  and  overriding  

•  C-­‐based  APIs  (20%  of  APIs)  –  Exposed  as  C#  classes/methods  – No  support  for  subclassing  or  overriding  

Page 8: Xamarin.Mac Seminar

The  Basics  

•  CoreGraphics  •  CoreImage  •  CoreText  •  CoreVideo  

Graphics  •  ImageKit  •  ImageIO  •  OpenGL  •  PDFKit  

•  AppKit  •  CoreAnima0on  •  CoreImage  

User  Interface  •  QCComposer  •  QuickLook  •  SceneKit  •  WebKit  

•  AVFounda0on  •  AudioToolbox  •  AudioUnit  

Audio  and  Video  •  CoreMidi  •  CoreMedia  •  OpenAL  

•  AddressBook  •  Bluetooth  •  CoreLoca0on  •  CoreServices  

System  Services  •  CoreWLan  •  Scrip0ngBridge  •  StoreKit  

•  CoreData  •  CoreFounda0on  •  Darwin  

Infrastructure  

•  Founda0on  •  ObjCRun0me  •  Security  

Page 9: Xamarin.Mac Seminar
Page 10: Xamarin.Mac Seminar

Anatomy  of  a  Xamarin.Mac  App  NSDocument  version  

 Applica'on  Delegate  Called  with  applica0on  events,  among  them    “FinishedLaunching”  

Info.plist  Applica0on  metadata,  used  by  the  OS  (app  name,  requirements,  doc  type  handlers,  etc)  

Main  applica'on  menu  Interface  defini0on  for  your  main  menu  

Implementa'on  for  your  main  window  Code  to  implement  the  features  of  your  Document  handler.  

Main  Window  UI  Defini'on  UI  for  your  Main  Window,  edited  with  Xcode.  

Page 11: Xamarin.Mac Seminar

Results  

Page 12: Xamarin.Mac Seminar

Structure  of  your  App  

NSWindow    The  toplevel  window  in  your  app    NSWindow.ContentView    An  NSViews,  hosts  all  content  

Page 13: Xamarin.Mac Seminar

NSViews  –  Powerful  containers  

•  NSViews  can  contain  other  NSViews  •  NSViews  can  handle  events  •  NSViews  can  paint  themselves  •  NSViews  can  layout  their  nested  NSViews  •  NSViews  can  be  backed  by  a  CALayer  

– CALayers  are  GPU  accelerated  •  NSView  proper0es  can  be  animated  

Page 14: Xamarin.Mac Seminar

NSWindow  and  nested  NSViews  

Page 15: Xamarin.Mac Seminar

AppKit  -­‐  Applica0on  Framework  

•  Pervasive  use  of  Model  View  Controller  – Unless  you  are  wri0ng  a  custom  control  – All  logic  goes  into  your  controller  class  – Controller  orchestrates  work  of  views  

•  Goes  beyond  the  standard  offering  – High-­‐level  NSDocument  does  the  heavy  lijing  – Full  Menu,  Saving,  Loading,  mul0-­‐window  support  

Page 16: Xamarin.Mac Seminar

Extending  our  first  Window  

Create  +  Ini0alize  Object  

Hook  up  some  code,  lambda  

Subclass  

Page 17: Xamarin.Mac Seminar

My  app  in  Ac0on  

Page 18: Xamarin.Mac Seminar

Crea0ng  Beau0ful  Interfaces  

•  Launch  Xcode  to  edit  XIB  files.  

•  Ac0vate  side-­‐by-­‐side    view          

•  Control-­‐drag  to  source  file.  

Page 19: Xamarin.Mac Seminar

Connec0ng  your  code  

Page 20: Xamarin.Mac Seminar

Exposing  the  UI  to  Code  •  Outlets  

–  Allows  you  to  reference  an  object  from  code  –  Control-­‐drag  to  source  pane,  and  give  it  a  name  

•  Ac0ons  –  Allows  a  method  to  be  invoked  as  a  response  –  Control  drag  to  source  pane,  and  give  a  name  

•  In  C#  land  –  Ac0ons  become  par0al  methods  in  your  class  –  Outlets  become  proper0es  in  your  class  

Page 21: Xamarin.Mac Seminar

Implemen0ng  Ac0ons  and  using  Outlets  

Page 22: Xamarin.Mac Seminar

Running  

Page 23: Xamarin.Mac Seminar

Behind  the  Scenes  

Page 24: Xamarin.Mac Seminar

Events  and  Callbacks  

•  In  the  C#  world  we  are  used  to  objects  broadcas0ng  events.      Like  this:  –  var  myControl  =  new  SomeControl  ();  –  myControl.Clicked  +=  SaveFile;  –  myControl.FocusIn  +=  MakeFontBold;  –  myControl.FocusOut  +=  ResetFont;    

MyControl  

SaveFile  

MakeFontBold  

ResetFont  

Page 25: Xamarin.Mac Seminar

Apple’s  Idiom  

•  Objects  instead  send  interes0ng  events  to  a  “Delegate”.      All  messages  are  sent  there.    var  myControl  =  new  SomeControl  ()  myControl.Delegate  =  new  MyControlDelegate  ()  

myControlDelegate    class  MyControlDelegate  :  ControlDelegate  {  

 override  Clicked  ()  {…}    override  FocusIn  ()  {…}    override  FocusOut  ()  {…}  

}  

myControl  

Page 26: Xamarin.Mac Seminar

Xamarin.Mac  and  Delegates  

•  Both  models  are  supported  – You  get  to  choose  in  a  per-­‐instance  basis  

•  Apple  Delegate  panern  mapped  to  C#  events  –  Internally,  we  create  the  Delegate  class,  map  it  to  your  C#  lambda/anonymous  method/delegate  

•  One  replaces  the  other  

Page 27: Xamarin.Mac Seminar

SHIPPING  YOUR  APP  

Page 28: Xamarin.Mac Seminar

Shipping  Your  App  -­‐  Yourself  •  App  has  full  access  to  the  system  

•  Applica0ons  are  shipped  as  “bundles”  – Directory  with  .app  extension  –  Self-­‐contained,  with  no  external  dependencies  – Op0onally:  generate  installer  from  IDE  as  well.  

•  MacOS  recently  enabled  GateKeeper  –  This  requires  you  to  sign  your  app  – Or  apps  wont  start  on  modern  systems1  (by  default)  

1.  Technically  any  Mac  running  Mountain  Lion  (10.8.xx)  or  Lion  10.7.5  and  newer  

Page 29: Xamarin.Mac Seminar

Shipping  your  App  -­‐  AppStore  •  Mac  App  Store  

– Must  apply  to  Apple  for  developer  program  – Must  sign  applica0on  –  Submit  app  for  review  

•  App  will  be  sandboxed  

•  IDE  signs,  packages  and  launches  uploader  

•  See  Xamarin’s  docs  for  tutorial  

Page 30: Xamarin.Mac Seminar

MacOS  X  Sandbox    

•  Kernel  enforced  sandbox  •  Limits  access  to  the  system:  

– Limita0ons  on  file  system  access  – Use  special  Open/Save  dialog  panels  – Limits  access  to  services,  and  some  kernel  APIs  

 

Page 31: Xamarin.Mac Seminar

Mac  AppStore  –  Sandbox  Configura0on  

Info.plist      Edi0ng  this  file  brings  up  the  metadata  editor.    Use  this  editor  to  configure  your  applica0on  requirements  from  the  sandbox.  

Page 32: Xamarin.Mac Seminar

Next Steps Learn more at: -  xamarin.com/mac -  Docs.xamarin.com

Free trial: -  xamarin.com/trial

April 14 – 17, Austin, TX 2 Days of Xamarin Technical Training & Certification 2 Conference days covering all things mobile Keynotes by Nat, Miguel and Scott Hanselman Call for speakers officially open & sponsorship information available at [email protected]

Deep Dive at Xamarin Evolve 2013

Page 33: Xamarin.Mac Seminar

Pricing  

•  Free  trial  at  xamarin.com/trial  

•  Professional:  $399    

•  Enterprise:  $999  per  seat  

•  Enterprise  Priority:  $2,499  per  seat  

Page 34: Xamarin.Mac Seminar

Resources  •  Xamarin’s  Mac  Resources:  

–  hnp://docs.xamarin.com/mac  

•  API  documenta0on:  –  Installed  on  first  use  (Apple  +  Xamarin  Docs)  

•  C#  samples:  hnps://github.com/xamarin/mac-­‐samples  

•  Support  Forum:  hnp://forums.xamarin.com/categories/mac  

•  Apple  Developer  Portal:  hnp://developer.apple.com/mac  

•  Xamarin’s  Evolve  conference:  hnp://xamarin.com/evolve  

Page 35: Xamarin.Mac Seminar

MORE  INFORMATION  

Page 36: Xamarin.Mac Seminar

TouchDraw  and  iCircuit  

•  TouchDraw  

 •  iCircuit  

Page 37: Xamarin.Mac Seminar

PROJECTIONS  

Page 38: Xamarin.Mac Seminar

Objec0ve-­‐C  Projec0on  to  C#  •  What  we  map:  

–  Classes    –  Structures  – Methods  and  Proper0es  –  Objec0ve-­‐C  blocks  –  Public  global  fields  –  No0fica0ons  –  Dic0onaries  –  Core  data  types  

•  Follow  the  .NET  Framework  Design  Guidelines  

Page 39: Xamarin.Mac Seminar

Projec0ng  Classes  and  Structs  

•  Iden0cal  class  names  •  Scoped  by  namespace      Example:    NSWindow  and  NSURL  

 Become  

 MonoMac.AppKit.NSWindow    MonoMac.Foundation.NSUrl  

Page 40: Xamarin.Mac Seminar

Projec0ng  Methods  and  Proper0es  

•  We  rename  methods  to  follow  .NET  FDG  •  Use  C#  overloading  

Example:  -­‐(void)  drawString:(NSString  *)  str  atPoint:(CGPoint)pos  -­‐(void)  drawString:(NSString  *)  str  

 Becomes:  void  DrawString  (string  str)  void  DrawString  (string  str,  PointF  position)  

 

Page 41: Xamarin.Mac Seminar

Projec0ng  Blocks  

•  Blocks  are  Objec0ve-­‐C  lambdas  •  They  are  mapped  to  C#  delegates  

– Can  be  used  with  C#  lambdas  or  methods  

Example:  (int)(^)(string  msg,  int  flag)  

Becomes:    delegate  int  X  (string  msg,  int  flag)  

Page 42: Xamarin.Mac Seminar

Projec0ng  No0fica0ons  

Regular  Style/Objec've-­‐C   Strongly  Typed  

•  No0fica0ons  are  posted  as  strings  +  Dic0onary  •  We  map  those  to  strong  types  +  EventArgs  

Page 43: Xamarin.Mac Seminar

Projec0ng  NSDic0onary  Op0ons  

•  Dic0onaries  are  used  to  pass  parameters  •  Loosely  typed,  hard  to  debug,  hard  to  find  

•  We  provide  strongly  typed  classes  •  They  implicitly  convert  to  NSDic0onary  

– With  the  right  magic  keys  – With  the  correct  types  for  parameters  

Page 44: Xamarin.Mac Seminar

AppKit  –  The  Heart  of  Mac  Apps