Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and...

29
Presented by Mir Majeed Cross-Platform Mobile Platforms and Xamarin

Transcript of Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and...

Page 1: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

Presented by Mir Majeed

Cross-Platform Mobile Platforms and Xamarin

Page 2: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

Agenda1. Sharing Code Among Different Platforms

• File-Linking into each App Project• Portable Class Libraries

2. Solution Population Strategies

3. Cross-Platform Mobile Development Frameworks1. MvvmCross2. MonoCross

Page 3: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

The Silo’d Approach: Build Apps Multiple Times

Page 4: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

File-Linking into each App ProjectThe simplest approach to sharing code files is to place them in a separate directory (ie. outside of your various mobile application projects) and use file-linking to include them.

Page 5: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

File-Linking into each App Project

Page 6: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

File-Linking into each App Project

Benefits•Allows you to share code across multiple projects.•Shared code can be branched based on the platform using compiler directives (eg. using #if __ANDROID__, as discussed in the Building Cross Platform Applications document).•Application projects can include platform-specific references that the shared code can utilize (such as using Community.CsharpSqlite.WP7 in the Tasky and MWC samples).

Disadvantages•In Visual Studio there is no simple way to add an entire filesystem ‘tree’ to a project, so files must be individually added. The directory hierarchy must also be manually created.•If you add, delete or move a file in one project you must remember to perform the same action in all the other project files.

Page 7: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

File-Linking into each App Project

•Refactoring will only work within one platform type.•Empty subdirectories must also exist before you can ‘link’ files contained within a subdirectory.•If you forget to choose Add As Link (in Visual Studio), it will copy the file into your target project instead of linking. If you don’t notice this has occurred you could accidentally edit both the original file and the copy, getting them out-of-sync.

Page 8: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

Portable Class Libraries (PCL)

•A Portable Class Library (PCL) is a special type of project that can be used across Different CLI platforms such as Xamarin.iOS and Xamarin.Android, as well as Silverlight, WPF, Windows Phone and Xbox. The library can only utilize a subset of the complete .NET framework, limited by the platforms being targeted.

Page 9: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

Portable Class Libraries (PCL)

The Xamarin column reflects the fact that Xamarin.iOS and Xamarin.Android supports all the profiles shipped with Visual Studio 2013, and the availability of features in any libraries you create will only be limited by the other platforms you choose to support (such as Windows Phone or Windows Store).

This includes profiles that are combinations of:• .NET 4 or .NET 4.5• Silverlight 5• Windows Phone 8• Windows Store Apps

Page 10: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

Portable Class Libraries (PCL)

Page 11: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

Portable Class Libraries (PCL)

Benefits•Centralized code sharing – write and test code in a single project that can be consumed by other libraries or applications.•Refactoring operations will affect all code loaded in the solution (the Portable Class Library and the platform-specific projects).•The PCL Project itself, or the output assembly can be shared.

Disadvantages•Because the same Portable Class Library is shared between multiple applications, platform-specific libraries cannot be referenced (eg. Community.CsharpSqlite.WP7).•The Portable Class Library subset may not include classes that would otherwise be available in both MonoTouch and Mono for Android (such as DllImport or System.IO.File).

Page 12: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

Solution Population Strategies

Two Project Types:•Core project – Write re-usable code in one place, to be shared across different platforms. Use the principles of encapsulation to hide implementation details wherever possible.

•Platform-specific application projects – Consume the re-usable code with as little coupling as possible. Platform-specific features are added at this level, built on components exposed in the Core project.

Page 13: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

Solution Population Strategies

Structure of Core Project•Shared projects should implement as much non-UI functionality as is possible, which generally includes the following layers:•Data Layer – Code that takes care of physical data or even XML files. The data layer classes are normally only used by the data access layer.•Data Access Layer – Defines an API that supports the required data operations for the application’s functionality, such as methods to access lists of data, individual data items and also•Service Access Layer – An optional layer to provide cloud services to the application. Contains code that accesses remote network resources (web services, image downloads, etc) and possibly caching of the results.•Business Layer – Definition of the Model classes and the Façade or Manager classes that expose functionality to the platform-specific applications.

Page 14: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

Solution Population Strategies

Structure of Platform-Specific Application Projects•Platform-specific projects must reference the assemblies required to bind to each platform’s SDK (Xamarin.iOS, Xamarin.Android or Windows Phone) as well as the Core shared code project.•Application Layer – Platform specific functionality and binding/conversion between the Business Layer objects and the user interface.•User Interface Layer – Screens, custom user-interface controls, presentation of validation logic.

Page 15: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

Solution Population Strategies

Page 16: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

MvvmCross

Page 17: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

About MvvmCross

• Fork of the MonoCross project• Started by Stuart Lodge• Moved from MVC and towards MVVM• It is an extension project from http://www.cirrious.com

Page 18: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

MvvmCross Manifesto

Portability – Use Portable Class Libraries for as much of your code as you possibly can -

viewmodel, model, service and even view.

Interface Driven Development – Use Dependency Injection, Inversion of Control and Plugins to

get your applications richly and robustly to market on all of your target platforms.

Code for Test – Use interfaces; develop small, cohesive, loosely coupled components.

Mvvm – Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

Native UIs – users love Native, and you should give them Native UIs that delight, that provide rich

functionality and that are styled to fit naturally in the context of your users’ devices.

Page 19: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.
Page 20: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.
Page 21: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

Supported Platforms

• Windows Phone 7 and Windows Phone 8

• Windows Presentation Foundation

• Windows Store

• Xamarin.Android

• Xamarin.iOS

• Xamarin.Mac

Page 22: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

Parts of MvvmCross Application

Core Project – containing all the ViewModels, Services, Models and 'business' code.

UI Project(s) – per platform containing the Views and platform specific code for

interacting with the Core Project.

Page 23: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

Key MvvmCross Objects

Core Project

• an App object - responsible for starting your ViewModels and your business logic

• a Start object - responsible for deciding the first ViewModel or ViewModels which

should be presented

• one or more ViewModels - each one responsible for a piece of user interaction

• Services and Models

Page 24: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

Key MvvmCross Objects

UI Project

• The native Application object - responsible for native lifecycle events - on each

platform this object is a platform-specific class

• MvvmCross Setup class - responsible for 'bootstrapping' MvvmCross, Core and UI

Projects

• One or more Views - each one responsible for presenting one of your ViewModels

• Custom UI code - for controls, gestures, events, etc.

Page 25: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.
Page 26: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

MonoCross

Page 27: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

About MonoCross

• MonoCross is an open source cross-platform mobile

framework using C# .NET and the Mono framework.

• It is based on Model-View-Controller (MVC) Pattern

enables cross-platform portability of business logic

and data access code, (Model + Controller), while

supporting full, native and/or platform-specific

presentation (Views).

Page 28: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.

MonoCross Solution Structure

• It consists of the MonoCross framework , a shared application, and one or more

container projects.

• The core application logic is contained in shared application which is referenced by

multiple class library assembly projects. This project structure enables code files to be

compiled to the appropriate platform.

Page 29: Cross-Platform Mobile Platforms and Xamarinfiles.meetup.com/12584322/Xamarin and CrossPlatform... · Mvvm –Use architectural patterns - especially Model-View-ViewModel with Data-Binding.