cross-platform apps using xamarin and mvvmcross - martijn van dijk - codemotion milan 2016

51
Cross-platform Apps using Xamarin and MvvmCross Martijn van Dijk MILAN 25-26 NOVEMBER 2016 github.com/martijn00 speakerdeck.com/ martijn00 @mhvdijk [email protected]

Upload: codemotion

Post on 13-Jan-2017

125 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

Cross-platform Apps using Xamarin and MvvmCrossMartijn van Dijk

MILAN 25-26 NOVEMBER 2016

github.com/martijn00speakerdeck.com/martijn00

@[email protected]

Page 2: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

2

Progress is teamworkCorporate introductionHelping you solve mobile challenges

Page 3: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

3

Stay currentWorks EverywhereNative5-Star App

Integration

Your priorities when building apps

Page 4: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

4

Approaches to mobile app development

Silo Black box

Page 5: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

5

Silo approach

iOS App

Obj-CSwift

XCode

Android AppJava

EclipseAndroidStudio

Windows AppC#

Visual Studio

Page 6: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

6

Black box approach

Black Box

Can build app using high-level tools that convert a single code base (typically in HTML/JavaScript) to an app for each platform

HTML

CSS

JavaScript

ActionScrip

t

LuaHTML

ActionScript

Page 7: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

77

Xamarin’s Mission

Make it fast, easy, and fun to create great mobile apps.

Page 8: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

8

Shared C# codebase • 100% native API access • High performance

iOS C# UI

Windows C# UIAndroid C# UI

Shared C# Mobile

Xamarin’s Unique Approach

Page 9: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

9

Completemobile lifecycle

Enterprise-grade

Xamarin

Page 10: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

1010

Anything you can do in Objective-C, Swift, or Java can be done in C# and Visual Studio with Xamarin.

Page 11: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

Let’s build an app,demo time!

11

Page 12: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

12

Architecture matters

■ Setting up a mobile app doing it ‘right’ is difficult

■ Take your time!■ Take your time!■ Take your time!

Building an app with/on the wrong foundation is going to cost you in the end

Page 13: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

13

Architecture: patterns

■ There are a few architectural design patterns that can be used.

■ Xamarin does not (en)force you to choose a particular one.

■ Most used are MVC and MVVM.■ It’s generally considered that MVVM suits Xamarin

the best.

Page 14: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

Why Mvvm?

View Binder ViewModel

Model

Button

Text

1. Action

1. Action

3. Command

5. Notify change

6. Change data

4. Access data

2. Event handling

14

Page 15: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

1515

MvvmCross Mission

Make quality apps with a high percentage shared code.

Page 16: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

The history ofMvvmCross

16

Page 17: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

Started by Stuart Lodge

Started as fork of MonoCross (Mvc framework)

November 2012

17

Page 18: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

Evolved into MvvmCross

Taken over by ● Tomasz Cielecki /

Cheesebaron

● Martijn van Dijk / Martijn00

+ Many others

June 2013

18

Page 19: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

Android support added

June 2015

Xamarin.Forms added

August 2015

Plugin structure added

September 2015

19

Page 20: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

iOS supportadded

February 2016

And more in the future!

20

Page 21: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

WhyMvvmCross?

21

Page 22: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

22

Cross platform awesomeness!

Support for all major platforms

Most advanced Mvvm library for

Xamarin and.NET cross platform

Page 23: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

23

Large and engaged community

Fast release cycle

Clean & easy conventions

Page 24: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

24

Page 25: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

I love MvvmCro

ss

I am really impressed

with MvvmCross

Miguel de IcazaXamarin CTO

Scott Hanselman

Microsoft Developer Evangelist

25

Page 26: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

MvvmCross Supported platforms■ Android■ iOS■ Windows■ Xamarin.Forms■ Mac■ And more! 26

Page 27: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

MvvmCross Highlights

■ Flexible architecture■ PCL based■ Inversion of Control■ Dependency injection

■ Value Converters■ Bindings■ Testable■ Plugins

27

Page 28: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

Let take a look at setting up a basic project

28

Page 29: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

2929

BindingsiOS:

var set = this.CreateBindingSet<MainViewController, MainViewModel>();set.Bind(Label).To(vm => vm.Title);set.Apply();

Android:

local:MvxBind="Text Title"

Page 30: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

30

Device & platform fragmentation

Page 31: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

31

■Customize view presentation■Platform-specific■Still retain View Model logic■Presentation hints

The solution?MvvmCross presenters

Page 32: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

3232

Let’s take a look at some samples

Page 33: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

3333

Hamburger menu

Segmented control

Tab bar

Page 34: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

3434

iOS presenter// Override the IMvxIosViewPresenter to customize iOS presentation

protected override IMvxIosViewPresenter CreatePresenter(){ return new MvxSidePanelsPresenter((MvxApplicationDelegate)ApplicationDelegate, Window);}// Use the Panel attribute to indicate where a viewcontroller is shown on the screen

[MvxPanelPresentation(MvxPanelEnum.Left, MvxPanelHintType.ActivePanel, false)]public class LeftPanelView : BaseViewController<LeftPanelViewModel>

Page 35: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

3535

Navigation drawer Tabs

Floating action button

Page 36: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

3636

Android fragment presenter// To use a custom presenter override the IMvxAndroidViewPresenter in your Android setup.csprotected override IMvxAndroidViewPresenter CreateViewPresenter(){ var mvxFragmentsPresenter = new MvxFragmentsPresenter(AndroidViewAssemblies); Mvx.RegisterSingleton<IMvxAndroidViewPresenter>(mvxFragmentsPresenter); return mvxFragmentsPresenter;}// Use the Fragment attribute to show fragments within an Android Activity

[MvxFragment(typeof (MainViewModel), Resource.Id.content_frame, false)][Register("com.sample.droid.fragments.LoginFragment")]public class LoginFragment : BaseFragment<LoginViewModel>

Page 37: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

37

■ Tabs / Panorama■ Split View / Master-Detail■ Fragments■ Modals■ Hamburger menu

Technical implementations

Page 38: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

3838

Enables the best Native UI

Build beautiful Native Apps using Xamarin and

MvvmCross!

Easy to implement custom presenters

Enables you to customize the behavior of your app without making your app

multiple times

Most code reuseUsing the presenters saves

you budget because you only need to code navigation once

Recap of MvvmCross presenters

Page 39: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

■MvvmCross uses CoC (Convention over Configuration) by default- LoginView > LoginViewModel

■Generics can be used too- LoginView : MvxActivity<LoginViewModel>- MainView : MvxActivity<SomeDifferentNameViewModel>

■Possible to override in setup.cs- protected override IDictionary<Type, Type>

GetViewModelViewLookup()39

Generics

Page 40: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

1.Use interfaces

2.Define implementation of interfaces at runtime

3.Job done! :)

■Singleton: Mvx.RegisterSingleton<T>();

■Lazy: Mvx.ConstructAndRegisterSingleton<T>();

■Dynamic: Mvx.RegisterType<T>();

Mvx.Resolve<T>();40

IoC (Inversion of Control)

Page 41: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

public class MyViewModel : MvxViewModel { public MyViewModel(IMvxJsonConverter jsonConverter, IMvxGeoLocationWatcher locationWatcher) { // Do stuff.... } }

41

Dependency Injection

Page 42: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

■ Accelerometer

■ Download Cache

■ Email

■ File

■ Json

■ Localization

■ Location

Plugins available athttps://github.com/MvvmCross/MvvmCross-Plugins

■ Messenger

■ Phone Call

■ Picture Chooser

■ SQLite

■ Visibility

■ Web Browser

+ Many More!42

MvvmCross Plugins

Page 43: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

public class LocationViewModel : MvxViewModel { private readonly MvxSubscriptionToken _token;

public LocationViewModel(IMvxMessenger messenger) { _token = messenger.Subscribe<LocationMessage>(OnLocationMessage); }

private void OnLocationMessage(LocationMessage locationMessage) { Lat = locationMessage.Lat; Lng = locationMessage.Lng; } }

43

Messenger

Page 44: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

■ V7 AppCompat

- MvxCachingFragmentActivity

- android:Theme

- Toolbar

- DrawerToggle

■ Core.UI

- SwipeRefresh

- DrawerLayout

■ Core.Utils

- WakefulBroadcastReceiver

■ Fragments

- Support Fragment

- ViewPager (FragmentStatePager)

■ RecyclerView

- ItemTouchHelper for Xamarin

- Multiple item templates

■ Design

- NavigationView

- FloatingActionButton

■ Compat

■ Media.Compat

■ LeanBack

■ Preference

44

Material Design support for MvvmCross

Page 45: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

See it in action,demo time!

45

Page 46: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

Other Material &Android support libraries

■ Cardview

■ Pallete

■ Gridlayout

■ Mediarouter

■ V8 Support

■ V13 Support

■ Annotations support

■ Custom tabs

■ Percent support

■ Recommendation support

46

Page 47: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

4747

Testingusing MvvmCross.Test.Core;using Moq;using NUnit.Framework;

[TestFixture]public class MyTest : MvxIoCSupportingTest{ [Test] public void TestViewModel() { base.Setup(); // from MvxIoCSupportingTest

// your test code }}

Page 48: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

■ Github.com/MvvmCross

■ MvvmCross.com■ Slack (#mvvmcross)■ Stackoverflow■ Xamarin Forums

MvvmCross Resources

48

Page 49: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

Tips

■ Keep it simple

■ Separation of Concerns

■ Don’t try to invent the wheel again, use plugins, samples, etc.

■ Use the Analysis plugin to fix common mistakes

49

Page 50: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

Get help on Slack

xamarinchat.herokuapp.com #MvvmCross channel

Follow influencers#MvvmCross

@Mhvdijk@Cheesebaron@MvvmCross

Join the LinkedIn group

linkedin.com/groups/8456977MvvmCross & Xamarin group

Contribute on Github

github.com/MvvmCross/MvvmCross

50

Get involved!

Page 51: Cross-platform Apps using Xamarin and MvvmCross - Martijn van Dijk - Codemotion Milan 2016

Questions?Martijn van Dijk

github.com/martijn00speakerdeck.com/martijn00

@[email protected]

51