104
LEARNING IPAD C# PROGRAMMING (REV 001) Rich Helton [email protected] August 28, 2012

Learning C# iPad Programming

Embed Size (px)

DESCRIPTION

Building IPAD Apps in MonoTouch

Citation preview

Page 1: Learning C# iPad Programming

LEARNINGIPADC#

PROGRAMMING(REV 001)

Rich [email protected]

August 28, 2012

Page 2: Learning C# iPad Programming

What is MonoDevelop?

• MonoDevelop is an open source integrated development environment for the Linux platform, MAC OS X, and Microsoft Windows, targeted for the development of software that uses Mono and the Micorosft .NET frameworks.

• See http://en.wikipedia.org/wiki/MonoDevelop

Page 3: Learning C# iPad Programming

What is Mono?

• Mono is a free open source project led by Xamerin (formally Novell and originally by Ximian) to create an Ecma standard compliant .Net framework-compatible set of tools including, among others, a C# compiler and Common Runtime Language.

• See http://en.wikipedia.org/wiki/Mono_(software)

• See http://www.mono-project.com/Main_Page

Page 4: Learning C# iPad Programming

Mono end-to-end

• Mono is a series of wrappers around Android, Mac, iOS, and more.

• There is the base Mono framework, the IDE, and MonoTouch is the framework for compiling on the iPad device.

• Pieces of code can be reused for other Mono frameworks.

• Just to get MAC diagnostics written in C# is enough to get me looking into it.

Page 5: Learning C# iPad Programming

Still iOS

• iOS is the operating system for the iPhone, iPod, and iPad.

• We cannot escape learning bits of iOS by focusing on Mono, because Mono is a wrapper around iOS, but it helps us focus on the C# frameworks instead of getting lost in iOS.

• Mono allows us to use C# frameworks around iOS and other operating systems.

Page 6: Learning C# iPad Programming

MonoGame

• MonoGame is an Open Source implementation of the Microsoft XNA 4 Framework.

• This allows us to build games in XNA for the iPad.

• http://monogame.codeplex.com/

Page 7: Learning C# iPad Programming

XNA

• Microsoft’s XNA is a set of tools with a managed runtime environment provided by Microsoft that facilitates video game development and management.

• http://en.wikipedia.org/wiki/Microsoft_XNA

Page 8: Learning C# iPad Programming

MonoMac

• MonoMac is the Mac OSX, not iOS, binding to .NET in Mono.

• By using MonoMac and MonoGame, XNA/C# games can be written for the Mac OSX, like Lion.

• http://www.mono-project.com/MonoMac

Page 9: Learning C# iPad Programming

MonoMac Sample• A MonoMac sample for WhereisMyMac has

geolocated my Mac and me.

Page 10: Learning C# iPad Programming

What is MonoTouch?

• MonoTouch allows developers to create C# and .NET based application that run on the IPhone.

• http://en.wikipedia.org/wiki/MonoTouch#MonoTouch

• There is a cost for licenses.• https://store.xamarin.com/

Page 11: Learning C# iPad Programming

How does MonoTouch work?

• The MonoTouch platform compiles directly to the ARM assembly code.

• Even things like generics, which used to rely on Just In Time (JIT) compilation are available via Mono’s Ahead Of Time (AOT) compiler.

• http://docs.xamarin.com/iOS/getting_started/Introduction_to_MonoTouch#How_Does_MonoTouch_Work.3f

Page 12: Learning C# iPad Programming

Default Assemblies

• There are many default assemblies in MonoTouch, many in support of XML, Linq, WCF, SQLite, Sockets and more.

• Assemblies can be added by adding DLL’s to the MonoTouch project as references.

• http://docs.xamarin.com/iOS/about/assemblies • http://iOSapi.xamarin.com/?link=root%3a%2fcl

asslib

Page 13: Learning C# iPad Programming

There are many Apps in the App Store• There are many Apps from MonoTouch in the

App Store, but there is always a concern when relying on a proprietary licensed product like MonoTouch, but I always say that it is better to have something done quickly than not at all.

• Sample App Store Apps http://xamarin.com/apps

Page 14: Learning C# iPad Programming

There are many Samples• https://github.com/xamarin/MonoTouch-sam

ples

• http://samples.xamarin.com/iOS • https://bitbucket.org/mrshrinkray/MonoTouc

h-samples/src

• http://MonoTouchexamples.com/• https://github.com/chrisntr/MonoTouch-Exa

mples

Page 15: Learning C# iPad Programming

MONODEVELOP IS NOT

VISUAL STUDIO

Page 16: Learning C# iPad Programming

There are many differences

• Just because something supports .Net 4, it doesn’t mean it supports Visual Studio 2010 functionality.

• It supports C# functionality wrapped around iOS.

• For Instance, I can set up a UnitTest directly with Visual Studio tools, while Mono uses Nunit, so just be aware that you may be missing Visual Studio features.

Page 17: Learning C# iPad Programming

There are many differences

• For Instance, I can set up a Test Project for Visual Studio Test,

Page 18: Learning C# iPad Programming

There are limitations

• Besides some of the Visual Studio plugins and extensions, MonoTouch does have C# language limitations, such as limited Generics Support.

• See http://docs.xamarin.com/iOS/about/limitations

Page 19: Learning C# iPad Programming

INSTALLATION

Page 20: Learning C# iPad Programming

You can play for free• An evaluation version of MonoTouch can be

used in the iPad simulator for the MAC OSX, but a license will have to be purchased when running the MonoTouch program on the physical device.

• The Trial can be downloaded from http://xamarin.com/trial/

Page 21: Learning C# iPad Programming

You can play for free• An evaluation version of MonoTouch can be

used in the iPad simulator for the MAC OSX, but a license will have to be purchased when running the MonoTouch program on the physical device.

• The Trial can be downloaded from http://xamarin.com/trial/

Page 22: Learning C# iPad Programming

Install…• Ensure Xcode is installed.• Install the Mono Framework• Install MonoDevelop• Install MonoTouch

Page 23: Learning C# iPad Programming

Ensure that Xcode is installed• Install the correct Xcode for the MAC OSX,

get Xcode from https://developer.apple.com/devcenter/iOS/index.action

• Check Xcode Installation by using the Terminal and typing “/Developer/usr/bin/xcodebuild –version”

Page 24: Learning C# iPad Programming

Install Mono• Install mono SDK from

http://www.go-mono.com/mono-downloads/download.html

• Install the MonoDevelop from http://monodevelop.com/

• Install MonoTouch http://xamarin.com/trial/

Page 25: Learning C# iPad Programming

MVC

Page 26: Learning C# iPad Programming

Model-View-Controller (MVC) The Model-View-Controller is the most common design pattern in Software Architecture. There is the .NET MVC3, or MVC4, framework that can be used to implement it in MonoTouch for iPad design as well as for ASP.NET design. An intro to MonoTouch MVC can be found at http://www.infoq.com/articles/MonoTouch-mvc

Page 27: Learning C# iPad Programming

Model-View-Controller (MVC)• Here are the pieces:

Page 28: Learning C# iPad Programming

UIKit MVC Microsoft’s MVC3 project is a ASP.NET implementation of MVC. For the iPad MVC, the iOS UIKIT will have to be used. When working with MonoTouch, a basic knowledge of iOS will have to be established. MonTouch adds C# .Net on top of iOS, it does not replace it. Let’s start with a MVC UIKit example.

Page 29: Learning C# iPad Programming

An MVC Example Microsoft’s MVC3 project is a ASP.NET implementation of MVC. For the iPad MVC, the iOS UIKIT will have to be used and it will use it from the Xcode installed on the MAC OSX. When working with MonoTouch, a basic knowledge of iOS will have to be established. MonTouch adds C# .Net on top of iOS, it does not replace it. http://docs.xamarin.com/iOS/getting_started/intro_to_mvc_in_iOS

Page 30: Learning C# iPad Programming

AppDelegate One of the classes defined in the UIKit is the AppDelegate which is automatically defined in all MonoTouch projects in Main.cs. Its function is to receive the notifications from the OS that may affect application. I like to the think of of it as the global handling of the application. http://escoz.com/blog/MonoTouch-uiapplication-and-appdelegate-explained/

Page 31: Learning C# iPad Programming

AppDelegate functions Normally, there will be a function named FinishedLaunching that will have “window.makeKeyAnd” that will display the window from MainWindow.xib.

Page 32: Learning C# iPad Programming

AppDelegate example

Page 33: Learning C# iPad Programming

Other Delegates iOS contains other delegates that are used to pass information between the application and OS. Examples are UIAccelerometerDelegate, MPMediaPickerControllerDelegate, MKMapViewDelegate, UITableViewDelegate and more. These Delegates may use Protocols and Events for some of their communication.http://docs.xamarin.com/iOS/tutorials/Events%

2c_Protocols_and_Delegates

Page 34: Learning C# iPad Programming

UIView UIView is the base class that contain all the screen elements and properties to be managed by the UIViewController. The UIView has many default subclasses that can be used, such as UIControl, UIScrollableView, UITableView and more. https://developer.apple.com/library/iOS/#documentation/uikit/reference/uiview_class/UIView/UIView.html

Page 35: Learning C# iPad Programming

Some UIView subclasses

Page 36: Learning C# iPad Programming

Notice that Interface Builder matches UIKit

Page 37: Learning C# iPad Programming

UIViewController A default controller that controls the view. It comes with a lifecycle for functions to check if the ViewDidload, ViewDidUnload, and more.https://developer.apple.com/library/iOS/#do

cumentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html

Page 38: Learning C# iPad Programming

UIViewController-> ViewDidLoad ViewDidLoad is very important because after the View loads, then many of the pieces are added here to the, like buttons, labels, and more views.

Page 39: Learning C# iPad Programming

Starting with Controllers Many controllers will be derived from the UIKit UIViewController for the main UI View. Some examples can be found at http://www.infoq.com/articles/MonoTouch-mvc and http://www.knowing.net/index.php/MonoTouch-custom-viewcontroller-adding-stepwise-subviews/

Page 40: Learning C# iPad Programming

Starting with Controllers Many controllers will be derived from the UIKit UIViewController for the main UI View. Some examples can be found at http://www.infoq.com/articles/MonoTouch-mvc and http://www.knowing.net/index.php/MonoTouch-custom-viewcontroller-adding-stepwise-subviews/

Page 41: Learning C# iPad Programming

NIB File The NIB file is the compiled set of XIB files, the XML, for the App. It will be packaged in the deployed App. http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html

Page 42: Learning C# iPad Programming

XIB File There are many XML file formats to help generate GUI’s, for example in Microsoft's WPF, there is the XAML file format. For iOS, it is the XIB file format. These file formats offer a good base to initialize the graphics based on the devices grid system, but tighter control of the graphics will rely on the MVC code.

Page 43: Learning C# iPad Programming

XIB Code Generation

http://docs.xamarin.com/iOS/advanced_topics/xib_code_generation

Page 44: Learning C# iPad Programming

Interface Builder The XIB file can be managed in Xcode’s Interface Builder.See http://en.wikipedia.org/wiki/Interface_Builder Pro’s and Cons of IB and XIB can be found at http://mobile.tutsplus.com/tutorials/iphone/interface-builder/

Page 45: Learning C# iPad Programming

Outlets An outlet connects Interface Builder to the code. It is is the plumbing for the code behind for the MonoTouch to Objective-C bridge. http://docs.xamarin.com/iOS/advanced_topics/xib_code_generation#Outlet_Properties

Page 46: Learning C# iPad Programming

Adding an Outlet An outlet can be added in the Interface Builder (IB) or programmatically. It can be added to the AppDelegate through IB. Programmatically, it can be added through the OutletAttribute:

[Outlet]UIButton ButtonTap { get; set; }

http://docs.xamarin.com/iOS/getting_started/hello_iphone#Adding_an_Outlet

Page 47: Learning C# iPad Programming

Actions Actions in Objective-C are equivalent to control events in C#. Actions deliver notification signals to various objects.http://docs.xamarin.com/iOS/tutorials/Event

s,_Protocols_and_Delegates

Page 48: Learning C# iPad Programming

Adding an Action An action can be added in the Interface Builder (IB) or programmatically. The IB will generate action code that can be done programmatically with the Action annotation:[Action ("actnButtonClick:")]partial void actnButtonClick (MonoTouch.Foundation.NSObject

sender);

http://docs.xamarin.com/iOS/getting_started/hello_iphone#Adding_an_Action

Page 49: Learning C# iPad Programming

FIRST APP

Page 50: Learning C# iPad Programming

MonoDevelop->New->Solution

Page 51: Learning C# iPad Programming

Many pieces are created The AppDelegate.cs, Main.cs, default Controllers, and default XIBs are created now. The iPad application should compile and run already.

Page 52: Learning C# iPad Programming

Many pieces are created

Page 53: Learning C# iPad Programming

Code was generated The AppDelegate.cs and Main.cs were generated to start the application. For the main screen, a MainViewController.xib, MainViewController.cs, and the code behind that links the MainViewController to the XIB, as MainViewController.designer.cs. A similar second screen is created when “info” is pushed as FlipSideViewController.

Page 54: Learning C# iPad Programming

Run-> Start Debugging (iPad Simulator Launches)

Page 55: Learning C# iPad Programming

The showInfo Action will call the showInfo function, calling the FlipsController

Page 56: Learning C# iPad Programming

We can see showInfo Action and MainController in the XIB view

Page 57: Learning C# iPad Programming

Double Click -> FlipsideViewController.xib(Xcode Interface Builder Launches)

Page 58: Learning C# iPad Programming

Add a Label

Page 59: Learning C# iPad Programming

Run again, now it has a Label

Page 60: Learning C# iPad Programming

There was a “done” button, notice the Action for done in the code behind and the function in the Controller

Page 61: Learning C# iPad Programming

Some notes … We just changed the FlipsideViewController.xib, which may update the interface to the FlipSideViewController with the code behind code FlipSideViewController.designer.cs.

Page 62: Learning C# iPad Programming

This also means that the label is in the XIB code and the object is embedded

Page 63: Learning C# iPad Programming

Let’s add a label programmatically in ViewDidLoad

Page 64: Learning C# iPad Programming

Executing it, we see the new (HomeView) label.

Page 65: Learning C# iPad Programming

ADDING A VIEW

Page 66: Learning C# iPad Programming

Add a ViewController to the Project

Page 67: Learning C# iPad Programming

Which creates a default template for both the controller and XIB.

Page 68: Learning C# iPad Programming

And an empty XIB that works as the View

Page 69: Learning C# iPad Programming

Xamarim Recipe for Add a New View

Page 70: Learning C# iPad Programming

Running the Add New View

Page 71: Learning C# iPad Programming

MONOTOUCH.DIALOG

Page 72: Learning C# iPad Programming

Intro MonoTouch.dialog is a framework for MonoTouch that can be found at https://github.com/migueldeicaza/MonoTouch.Dialog The purpose of MonoTouch.Dialog is to save time in building dialog boxes and showing table based information.

Page 73: Learning C# iPad Programming

Many Demo Apps

Page 74: Learning C# iPad Programming

Switching it to iPad

Page 75: Learning C# iPad Programming

Demos in iPad

Page 76: Learning C# iPad Programming

Xamarin offers a Tutorial

Page 77: Learning C# iPad Programming

MonoTouch Elements(Extended classes of MonoTouch.Dialog)

Page 78: Learning C# iPad Programming

MonoTouch Elements Running

Page 79: Learning C# iPad Programming

Xamarin Elements Tutorial

Page 80: Learning C# iPad Programming

Xamarin Reflections Tutorial

Page 81: Learning C# iPad Programming

Running Reflections Tutorial

Page 82: Learning C# iPad Programming

Reflections Tutorial Notes

• Notice that there is no XIB file or Controller class.

• There is a decorated class with the attribute values.

• A UITableView will be created with section SectionAttributes.

Page 83: Learning C# iPad Programming

Set the values for the Dialog

Page 84: Learning C# iPad Programming

Create the Controller Flow for the Dialog in the AppDelegate

Page 85: Learning C# iPad Programming

WORKING WITH TABLES AND CELLS

Page 86: Learning C# iPad Programming

Tables and Cells Intro

• UITableView is the most common layout and data display tool in iOS.

• A normal display could be simply displaying the rows of tables.

• See http://docs.xamarin.com/iOS/tutorials/Working_with_Tables_and_Cells

Page 87: Learning C# iPad Programming

Xamarin Tutorial

Page 88: Learning C# iPad Programming

Running the Basic Table

Page 89: Learning C# iPad Programming

Running the Table Edit Mode

Page 90: Learning C# iPad Programming

INTRO TO STORYBOARDS

Page 91: Learning C# iPad Programming

Storyboards -- Apple

• In iOS5, Apple introduced a concept called Storyboards.

• A Storyboard is a visual representation of the user interface of an iOS application.

• See http://developer.apple.com/library/mac/#documentation/General/Conceptual/Devpedia-CocoaApp/Storyboard.html

Page 92: Learning C# iPad Programming

Storyboards -- MonoTouch

• Xamarin offers a Tutorial and sample code for a Storyboard.

• See http://docs.xamarin.com/ios/tutorials/Introduction_to_Storyboards

Page 93: Learning C# iPad Programming

Storyboards – The sample

• The sample will show Controllers, AppDelegate, plist, and Main as normal, but there is a also a storyboard file.

Page 94: Learning C# iPad Programming

The Storyboard file in Xcode

Page 95: Learning C# iPad Programming

Storyboards -- segue

• The segue manages the transitions between the scenes and passes data with a “sender”.

Page 96: Learning C# iPad Programming

Storyboards -- dock

• The dock is used to define outlets and actions between the view controller and views.

Page 97: Learning C# iPad Programming

Storyboards -- dock• The iPad can have multiple scenes appear at

once, but the scene is similar to a View.

Page 98: Learning C# iPad Programming

Running the sample shows navigation

Page 99: Learning C# iPad Programming

COMMANDSAND TOOLS

Page 100: Learning C# iPad Programming

mono has several command line tools

Page 101: Learning C# iPad Programming

mtouch Commands

Page 102: Learning C# iPad Programming

Multiple IDE’s

• Multiple MonoDevoleps can be run on Mac OSX with “open -n /Applications/MonoDevelop.app”

• See http://microsoft2apple.com/2010/09/26/multiple-instances-of-monodevelop-on-a-mac/

Page 103: Learning C# iPad Programming

MonoTouch Profiler

Page 104: Learning C# iPad Programming

CONCLUSION