xamarin.mac introduction
DESCRIPTION
Learn to write native Mac apps using C# and .NET directly from Miguel de Icaza, cofounder and CTO of Xamarin.TRANSCRIPT
![Page 2: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/2.jpg)
Xamarin.Mac Overview
• Build Mac applications 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
• Native Applications– Use native APIs to deeply integrate with OSX
![Page 3: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/3.jpg)
Xamarin.Mac Overview
2.2B devices reachable with C#
![Page 4: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/4.jpg)
Xamarin.Mac at a Glance
Mono Runtime
Xamaric.MacLibraries
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 Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/5.jpg)
Xamarin.Mac Frameworks
• CoreGraphics• CoreImage• CoreText• CoreVideo
Graphics• ImageKit• ImageIO• OpenGL• PDFKit
• AppKit• CoreAnimation• CoreImage
User Interface• QCComposer• QuickLook• SceneKit• WebKit
• AVFoundation• AudioToolbox• AudioUnit
Audio and Video• CoreMidi• CoreMedia• OpenAL
• AddressBook• Bluetooth• CoreLocation• CoreServices
System Services• CoreWLan• ScriptingBridge• StoreKit
• CoreData• CoreFoundation• Darwin
Infrastructure
• Foundation• ObjCRuntime• Security
![Page 6: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/6.jpg)
Lots shared with MonoTouch (iOS)
• CoreGraphics• CoreImage• CoreText• CoreVideo
Graphics• ImageKit• ImageIO• OpenGL• PDFKit
• AppKit• CoreAnimation• CoreGraphics• CoreImage
User Interface• QCComposer• QuickLook• SceneKit• WebKit
• AVFoundation• AudioToolbox• AudioUnit
Audio and Video• CoreMidi• CoreMedia• OpenAL
• AddressBook• Bluetooth• CoreLocation• CoreServices
System Services• CoreWLan• ScriptingBridge• StoreKit
• CoreData• CoreFoundation• Darwin
Infrastructure
• Foundation• ObjCRuntime• Security
![Page 7: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/7.jpg)
How does Xamarin.Mac work?
• OSX Libraries Projected to C#– 1:1 mapping from OSX native APIs to C#
• Objective-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 Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/8.jpg)
The Basics
• CoreGraphics• CoreImage• CoreText• CoreVideo
Graphics• ImageKit• ImageIO• OpenGL• PDFKit
• AppKit• CoreAnimation• CoreImage
User Interface• QCComposer• QuickLook• SceneKit• WebKit
• AVFoundation• AudioToolbox• AudioUnit
Audio and Video• CoreMidi• CoreMedia• OpenAL
• AddressBook• Bluetooth• CoreLocation• CoreServices
System Services• CoreWLan• ScriptingBridge• StoreKit
• CoreData• CoreFoundation• Darwin
Infrastructure
• Foundation• ObjCRuntime• Security
![Page 9: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/9.jpg)
![Page 10: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/10.jpg)
Anatomy of a Xamarin.Mac AppNSDocument version
Application DelegateCalled with application events, among them “FinishedLaunching”
Info.plistApplication metadata, used by the OS(app name, requirements, doc type handlers, etc)
Main application menuInterface definition for your main menu
Implementation for your main windowCode to implement the features of yourDocument handler.
Main Window UI DefinitionUI for your Main Window, edited withXcode.
![Page 11: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/11.jpg)
Results
![Page 12: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/12.jpg)
Structure of your App
NSWindow
The toplevel window in your app
NSWindow.ContentView
An NSViews, hosts all content
![Page 13: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/13.jpg)
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 properties can be animated
![Page 14: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/14.jpg)
NSWindow and nested NSViews
![Page 15: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/15.jpg)
AppKit - Application Framework
• Pervasive use of Model View Controller– Unless you are writing 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 lifting– Full Menu, Saving, Loading, multi-window support
![Page 16: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/16.jpg)
Extending our first Window
Create + Initialize Object
Hook up some code, lambda
Subclass
![Page 17: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/17.jpg)
My app in Action
![Page 18: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/18.jpg)
Creating Beautiful Interfaces
• Launch Xcode to editXIB files.
• Activate side-by-side view
• Control-drag to sourcefile.
![Page 19: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/19.jpg)
Connecting your code
![Page 20: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/20.jpg)
Exposing the UI to Code
• Outlets– Allows you to reference an object from code– Control-drag to source pane, and give it a name
• Actions– Allows a method to be invoked as a response– Control drag to source pane, and give a name
• In C# land– Actions become partial methods in your class– Outlets become properties in your class
![Page 21: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/21.jpg)
Implementing Actions and using Outlets
![Page 22: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/22.jpg)
Running
![Page 23: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/23.jpg)
Behind the Scenes
![Page 24: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/24.jpg)
Events and Callbacks
• In the C# world we are used to objects broadcasting events. Like this:– var myControl = new SomeControl ();– myControl.Clicked += SaveFile;– myControl.FocusIn += MakeFontBold;– myControl.FocusOut += ResetFont;
MyControl
SaveFile
MakeFontBold
ResetFont
![Page 25: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/25.jpg)
Apple’s Idiom
• Objects instead send interesting 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 Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/26.jpg)
Xamarin.Mac and Delegates
• Both models are supported– You get to choose in a per-instance basis
• Apple Delegate pattern 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 Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/27.jpg)
SHIPPING YOUR APP
![Page 28: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/28.jpg)
Shipping Your App - Yourself
• App has full access to the system
• Applications are shipped as “bundles”– Directory with .app extension– Self-contained, with no external dependencies– Optionally: 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 Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/29.jpg)
Shipping your App - AppStore
• Mac App Store– Must apply to Apple for developer program– Must sign application– Submit app for review
• App will be sandboxed
• IDE signs, packages and launches uploader
• See Xamarin’s docs for tutorial
![Page 30: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/30.jpg)
MacOS X Sandbox
• Kernel enforced sandbox• Limits access to the system:– Limitations on file system access– Use special Open/Save dialog panels– Limits access to services, and some kernel APIs
![Page 31: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/31.jpg)
Mac AppStore – Sandbox Configuration
Info.plist
Editing this file brings upthe metadata editor.
Use this editor to configureyour application requirementsfrom the sandbox.
![Page 32: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/32.jpg)
Next Steps
Learn more at:- xamarin.com/ma
c- Docs.xamarin.co
m
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 Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/33.jpg)
Pricing
• Free trial at xamarin.com/trial
• Professional: $399
• Enterprise: $999 per seat
• Enterprise Priority: $2,499 per seat
![Page 34: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/34.jpg)
Resources• Xamarin’s Mac Resources:
– http://docs.xamarin.com/mac
• API documentation:– Installed on first use (Apple + Xamarin Docs)
• C# samples: https://github.com/xamarin/mac-samples
• Support Forum: http://forums.xamarin.com/categories/mac
• Apple Developer Portal: http://developer.apple.com/mac
• Xamarin’s Evolve conference: http://xamarin.com/evolve
![Page 35: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/35.jpg)
MORE INFORMATION
![Page 36: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/36.jpg)
TouchDraw and iCircuit
• TouchDraw
• iCircuit
![Page 37: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/37.jpg)
PROJECTIONS
![Page 38: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/38.jpg)
Objective-C Projection to C#
• What we map:– Classes – Structures– Methods and Properties– Objective-C blocks– Public global fields– Notifications– Dictionaries– Core data types
• Follow the .NET Framework Design Guidelines
![Page 39: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/39.jpg)
Projecting Classes and Structs
• Identical class names• Scoped by namespace
Example:NSWindow and NSURL
BecomeMonoMac.AppKit.NSWindowMonoMac.Foundation.NSUrl
![Page 40: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/40.jpg)
Projecting Methods and Properties
• 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 Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/41.jpg)
Projecting Blocks
• Blocks are Objective-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 Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/42.jpg)
Projecting Notifications
Regular Style/Objective-C Strongly Typed
• Notifications are posted as strings + Dictionary• We map those to strong types + EventArgs
![Page 43: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/43.jpg)
Projecting NSDictionary Options
• Dictionaries are used to pass parameters• Loosely typed, hard to debug, hard to find
• We provide strongly typed classes• They implicitly convert to NSDictionary– With the right magic keys– With the correct types for parameters
![Page 44: Xamarin.Mac Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022081414/54b4f2da4a795994458b47cd/html5/thumbnails/44.jpg)
AppKit – The Heart of Mac Apps