70
Building Cross- Platform Apps with Xamarin and MvvmCross Flavius-Radu DEMIAN

C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Embed Size (px)

Citation preview

Page 1: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

 Building Cross-Platform Apps with Xamarin and

MvvmCrossFlavius-Radu DEMIAN

Page 2: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

A bit about me

• General Manager@Deventure

• Timisoara .Net Meetup organizer

• Mobile and Web developer

• Xamarin and Umbraco enthusiast

• In love with technology

[email protected] @flaviusdemian

Page 3: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Agenda

Mobile Development ApproachesXamarin’s Unique ApproachThe Xamarin Magic How it Works on iOSHow it Works on AndroidCode Sharing Techniques

Page 4: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Agenda

What is MVVM?Why MVVM?The MvvmCross magic Data-Binding, Commands, NavigationAdvantagesDisadvantages

Page 5: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Expectations

I wish to have an interactive presentation.Please feel free to ask questions any time.

After this presentation, you should know:• what Xamarin is and how to use it• the advantages and disadvantages of Xamarin• what is MVVM• what Mvvmcross is and how to use it

Page 6: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Expectations

My ultimate goal is to make you

curios.

Go home and try it yourself!

Page 7: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Mobile Development Approaches

Page 8: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Mobile Development ApproachesSilo Approach

Page 9: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Silo Approach

• each platform with it’s own stack – in it’s own silo• no code sharing• multiple teams => many developers• multiple codebase => synchronization problem• different toolsets

Page 10: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Mobile Development ApproachesWeb Approach

Page 11: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Web Approach“Write once, run anywhere”.

• no apps to deploy to the stores• shared codebase• basically a native app with a Webview• native look is simulated with CSS• most times you can tell it is written in HTML• fewer developers

Page 12: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Xamarin’s Unique Approach

Page 13: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Xamarin’s Unique Approach

Page 14: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Xama…what?

Page 15: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Xamarin Timeline

Flavius-Radu Demian
Now they surpassed the 1.2 million developers barrier
Page 16: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Xamarin’s Unique Approach

• native user interface• native performance• shared code across platforms• usage of C# & .NET framework• full API coverage• fewer developers

Page 17: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Xamarin’s Unique Approach

Probably the best way in a “Mobile First. Cloud First. People First” Satya Nadella

Page 18: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

The Xamarin MagicWrite Everything in C#

Page 19: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

The Xamarin Magic

.NET + iOS APIs100% coverage

Page 20: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

The Xamarin Magic

.NET + Android APIs100% coverage

Page 21: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

The Xamarin Magic

.NET + Windows APIs

Page 22: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

The Xamarin MagicXamarin takes the C# code and it compiles it down to native on iOS and Android.

The magic is that you can also use it on the Windows platform.

(Almost) everything you can do in Objective-C, Swift or Java can be also done in C# with Xamarin.Check the limitations on www.xamarin.com .

Since you use C# on all the platforms, code reuse varies from 60% to 90%.

Page 23: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

JIT compilationAOT compilation

Page 24: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

How Xamarin Works on iOS

Page 25: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

iOS Build + Execution Model

• code runs as 100% native app

• full Ahead of Time (AOT) compilation to produce an ARM binary for Apple’s App Store

• Mono Framework included

• exposes CocoaTouch SDK

Page 26: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

iOS Build + Execution Model

We can take advantage of the APIs exposed by Mono besides the fully covered iOS APIs.

Since we have Mono involved, we can use Portable Class Libraries (PCLs).

Page 27: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

How Xamarin Works on Android

Page 28: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Android Build + Execution Model

Xamarin.Android takes advantage of Just In Time (JIT) compilation on the Android device.

Page 29: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Android Build + Execution Model

• MCW – managed callable wrappers,

mostly managed by Mono.Android.dll

• ACW – Android callable wrappers

Page 30: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Always Up-to-DateXamarin.iOS offered same-day support for :

iOS 5 / iOS 6 / iOS 7 / iOS 7.1 / iOS 8 / iOS 9

Xamarin offers full support for: • Google Glass• Android Wear• Amazon Fire TV • Apple Watch and more

Page 31: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Development Environments

Page 32: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Visual Studio 201X

Page 33: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Xamarin Studio (PC + Mac)

Page 34: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Code Sharing Techniques

Page 35: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Code Sharing Techniques

There are multiple ways in which you can share code:• portable class libraries (PCLs)• partial classes• compiler conditional blocks• shared projects

Page 36: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Xamarin Architecture Guide

Page 37: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Xamarin Architecture Guide

Page 38: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Demo Time

Page 39: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

What is MVVM?

Page 40: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

MVVM vs MVC

In the MVVM the logic is stored in the presenter and the view is completely isolated from the model. 

Page 41: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Model View ViewModel

Page 42: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

MVVM Technical DetailsIt is derived from the Model View Controller Pattern.

• properties• data binding• INotifyPropertyChanged• INotifyCollectionChanged• IValueConverter• ICommand• UI thread

Page 43: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Why MVVM?

Page 44: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

MVVM Pros and ConsThere is a reason why it’s called a pattern.Pros:• separation of concerns• decoupling• code reuse• testability

Cons:• MVVM is "overkill" for simple UI operations

Page 45: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

The MvvmCross Magic

Page 46: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

The MvvmCross Magic

It’s an open-source framework maintained by Stuart Lodge & Co.

It has Dependency Injection built-in -> Interface Driven Design.

It has a lot of plugins already written such as location, emails, gallery, network, SQLite, secure storage, phone calls etc.

#IF plugins -> less work for the developer, but also less control.

Page 47: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

The MvvmCross Magic

Page 48: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

The MvvmCross Magic

MvvmCross has support for:• Xamarin.iOS• Mac• Xamarin.Android• Windows Presentation Foundation• Windows Phone• Windows 8• Universal Windows Platform

Page 49: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

The MvvmCross MagicIt has huge popularity and the community is very active.

https://jabbr.net/#/rooms/mvvmcrosshttps://xamarinchat.slack.com/messages/mvvmcross/https://github.com/MvvmCross/MvvmCross

Page 50: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Data Binding, Commands, Navigation

Page 51: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Data Binding - Properties

No C#, just XAML

<TextBox Text="{Binding Password, Mode=TwoWay}" />

Page 52: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Data Binding - Properties

No C#, just XML

xmlns:local="http://schemas.android.com/apk/res-auto"<EditText local:MvxBind="Text Password"

Page 53: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Data Binding - Properties

Just C#

var set = this.CreateBindingSet<LoginViewController, LoginViewModel>(); set.Bind(textField_Password).To(vm => vm.Password);

Page 54: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Data Binding - ListviewsNo C#, just XAML

<ListBox ItemsSource="{Binding MyCollection}"<ListBox.ItemTemplate>

<DataTemplate> …..</DataTemplate>

</ListBox.ItemTemplate></ListBox>

Page 55: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Data Binding - Listviews

No C#, just XML

<Mvx.MvxListView local:MvxBind="ItemsSource MyCollection; ItemClick ViewDetailsCommand"local:MvxItemTemplate="@layout/item_template_row" />

Page 56: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Data Binding - Listviews

Just C#

var set = this.CreateBindingSet<XViewController, XViewModel>();set.Bind(source).To(x => x.MyCollection);set.Apply();

Page 57: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

ViewModel Commands

No C#, just XAML

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"xmlns:commandbinding="clr-namespace:Cirrious.MvvmCross.WindowsPhone.Commands;assembly=Cirrious.MvvmCross.WindowsPhone" <TextBlock Text="{Binding Name}">

<i:Interaction.Triggers><i:EventTrigger EventName="Tap">

<commandbinding:MvxEventToCommand Command="{Binding MyCommand}" />

</i:EventTrigger></i:Interaction.Triggers>

</TextBlock>

Page 58: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

ViewModel Commands

No C#, just XML

xmlns:local="http://schemas.android.com/apk/res-auto"<Button local:MvxBind="Click LoginCommand" />

Page 59: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

ViewModel Commands

Just C#

var set = this.CreateBindingSet<LoginViewController, LoginViewModel>();set.Bind(btn_Login).To(x => x.LoginCommand);set.Apply();

Page 60: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Navigation

Go forward:CurrentViewModelInstance.ShowViewModel<NewViewModel>();

Page 61: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Navigation

Go back:CurrentViewModelInstance.Close(this);

Page 62: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

MvvmCross App Architecture

Page 63: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

App ArchitectureThe project should be split in 2:

Common core which contains:• models• viewmodels• business logic• data access layer

UI-project per platform:• each platform views

Page 64: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Small Demo Time

Page 65: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

MvvmCross Pros & Cons

Pros:• interchangeable code module• supports Test Driven Development (TDD)• it follows the Core pattern• it has data binding

Page 66: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

MvvmCross Pros & Cons

Cons:• it is a non-native pattern for iOS -MVC- and Android -

aprox MVC-• core 3rd party dependency• lot’s of glue code for custom bindings

Page 67: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Xamarin Pros & Cons

Pros:• native experience• you can use a lot of components that .NET developers

already use• shared code base• only one programming language

Page 68: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Xamarin Pros & Cons

Cons:• you need a Mac• it has a license fee per developer, per platform -> $• bugs can appear: https://bugzilla.xamarin.com/• C# is needed

Page 69: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Xamarin Pros

Page 70: C# everywhere - Building Cross-Platform Apps with Xamarin and MvvmCross

Thank you very much!