Xamarin.Mac Introduction

4,248 views
4,041 views

Published on

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

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
4,248
On SlideShare
0
From Embeds
0
Number of Embeds
1,135
Actions
Shares
0
Downloads
19
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Basically, it is what dreams are made of.
  • Xamarin.Mac Introduction

    1. 1. Xamarin.Mac Introduction Miguel de Icaza, CTO Xamarin miguel@xamarin.com January 10th 2013
    2. 2. 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
    3. 3. Xamarin.Mac at a Glance Xcode MonoDevelop IDE (UI Designer) Xamaric.Mac Cocoa Frameworks Tools and SDK Libraries• Binder Mono Core Class• Bundler System Libraries Libraries• Linker• Packager Mono Runtime Darwin OS
    4. 4. Xamarin.Mac Frameworks Graphics User Interface• CoreGraphics • ImageKit • AppKit • QCComposer• CoreImage • ImageIO • CoreAnimation • QuickLook• CoreText • OpenGL • CoreImage • SceneKit• CoreVideo • PDFKit • WebKit Audio and Video System Services • AddressBook • CoreWLan• AVFoundation • CoreMidi • Bluetooth • ScriptingBridge• AudioToolbox • CoreMedia • OpenAL • CoreLocation • StoreKit• AudioUnit • CoreServices Infrastructure• CoreData • Foundation• CoreFoundation • ObjCRuntime• Darwin • Security
    5. 5. Lots shared with MonoTouch (iOS) Graphics User Interface• CoreGraphics • ImageKit • AppKit • QCComposer• CoreImage • ImageIO • CoreAnimation • QuickLook• CoreText • OpenGL • CoreGraphics • SceneKit• CoreVideo • PDFKit • CoreImage • WebKit Audio and Video System Services • AddressBook • CoreWLan• AVFoundation • CoreMidi • Bluetooth • ScriptingBridge• AudioToolbox • CoreMedia • OpenAL • CoreLocation • StoreKit• AudioUnit • CoreServices Infrastructure• CoreData • Foundation• CoreFoundation • ObjCRuntime• Darwin • Security
    6. 6. 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
    7. 7. The Basics Graphics User Interface• CoreGraphics • ImageKit • AppKit • QCComposer• CoreImage • ImageIO • CoreAnimation • QuickLook• CoreText • OpenGL • CoreImage • SceneKit• CoreVideo • PDFKit • WebKit Audio and Video System Services • AddressBook • CoreWLan• AVFoundation • CoreMidi • Bluetooth • ScriptingBridge• AudioToolbox • CoreMedia • OpenAL • CoreLocation • StoreKit• AudioUnit • CoreServices Infrastructure• CoreData • Foundation• CoreFoundation • ObjCRuntime• Darwin • Security
    8. 8. Anatomy of a Xamarin.Mac App NSDocument version Application Delegate Called with application events, among them “FinishedLaunching” Info.plist Application metadata, used by the OS (app name, requirements, doc type handlers, etc) Main application menu Interface definition for your main menu Main Window UI Definition UI for your Main Window, edited withImplementation for your main window Xcode.Code to implement the features of yourDocument handler.
    9. 9. Results
    10. 10. Structure of your App NSWindow The toplevel window in your app NSWindow.ContentView An NSViews, hosts all content
    11. 11. 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
    12. 12. NSWindow and nested NSViews
    13. 13. 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
    14. 14. Extending our first Window Subclass Create + Initialize Object Hook up some code, lambda
    15. 15. My app in Action
    16. 16. Creating Beautiful Interfaces • Launch Xcode to edit XIB files. • Activate side-by-side view • Control-drag to source file.
    17. 17. Connecting your code
    18. 18. 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
    19. 19. Implementing Actions and using Outlets
    20. 20. Running
    21. 21. Behind the Scenes
    22. 22. 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; SaveFile MyControl MakeFontBold ResetFont
    23. 23. Apple’s Idiom• Objects instead send interesting events to a “Delegate”. All messages are sent there. var myControl = new SomeControl () myControl.Delegate = new MyControlDelegate () myControl myControlDelegate class MyControlDelegate : ControlDelegate { override Clicked () {…} override FocusIn () {…} override FocusOut () {…} }
    24. 24. 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
    25. 25. SHIPPING YOUR APP
    26. 26. 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
    27. 27. 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
    28. 28. 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
    29. 29. Mac AppStore – Sandbox ConfigurationInfo.plistEditing this file brings upthe metadata editor.Use this editor to configureyour application requirementsfrom the sandbox.
    30. 30. Pricing• Free trial at xamarin.com/trial• Professional: $399• Enterprise: $999 per seat• Enterprise Priority: $2,499 per seat
    31. 31. 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
    32. 32. MORE INFORMATION
    33. 33. TouchDraw and iCircuit• TouchDraw• iCircuit
    34. 34. PROJECTIONS
    35. 35. 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
    36. 36. Projecting Classes and Structs• Identical class names• Scoped by namespaceExample: NSWindow and NSURLBecome MonoMac.AppKit.NSWindow MonoMac.Foundation.NSUrl
    37. 37. Projecting Methods and Properties• We rename methods to follow .NET FDG• Use C# overloadingExample:-(void) drawString:(NSString *) str atPoint:(CGPoint)pos-(void) drawString:(NSString *) strBecomes:void DrawString (string str)void DrawString (string str, PointF position)
    38. 38. Projecting Blocks• Blocks are Objective-C lambdas• They are mapped to C# delegates – Can be used with C# lambdas or methodsExample: (int)(^)(string msg, int flag)Becomes: delegate int X (string msg, int flag)
    39. 39. Projecting Notifications• Notifications are posted as strings + Dictionary• We map those to strong types + EventArgsRegular Style/Objective-C Strongly Typed
    40. 40. 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
    41. 41. AppKit – The Heart of Mac Apps

    ×