Cross-platform Apps using Xamarin and MvvmCross
Martijn van Dijk
MILAN 25-26 NOVEMBER 2016
github.com/martijn00
speakerdeck.com/martijn00
@mhvdijk
mhvdijk@gmail.com
2
Progress is teamwork
Corporate introduction
Helping you solve mobile challenges
3
Stay currentWorks EverywhereNative
5-Star App
Integration
Your priorities when building apps
4
Approaches to mobile app development
Silo
Black box
5
Silo approach
iOS App
Obj-C
Swift
XCode
Android App
Java
Eclipse
AndroidStudio
Windows App
C#
Visual Studio
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
77
Xamarin’s Mission
Make it fast, easy, and fun to
create great mobile apps.
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
9
Complete
mobile lifecycle
Enterprise-grade
Xamarin
1010
Anything you can do in Objective-C,
Swift, or Java can be done in C# and
Visual Studio with Xamarin.
Let’s build an app,
demo time!
11
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
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.
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
1515
MvvmCross Mission
Make quality apps with a high
percentage shared code.
The history of
MvvmCross
16
Started by
Stuart Lodge
Started as fork of
MonoCross (Mvc framework)
November 2012
17
Evolved into
MvvmCross
Taken over by
● Tomasz Cielecki / Cheesebaron
● Martijn van Dijk / Martijn00
+ Many others
June 2013
18
Android support
added
June 2015
Xamarin.Forms
added
August 2015
Plugin structure
added
September 2015
19
iOS support
added
February 2016 And more in the future!
20
Why
MvvmCross?
21
22
Cross platform
awesomeness!
Support for all major
platforms
Most advanced Mvvm
library for Xamarin and
.NET cross platform
23
Large and engaged
community
Fast release cycle
Clean & easy conventions
24
I love
MvvmCross
I am really
impressed
with
MvvmCross
Miguel de Icaza
Xamarin CTO
Scott Hanselman
Microsoft Developer
Evangelist
25
MvvmCross
Supported platforms
■ Android
■ iOS
■ Windows
■ Xamarin.Forms
■ Mac
■ And more! 26
MvvmCross
Highlights
■ Flexible architecture
■ PCL based
■ Inversion of Control
■ Dependency injection
■ Value Converters
■ Bindings
■ Testable
■ Plugins
27
Let take a look
at setting up a
basic project
28
2929
Bindings
iOS:
var set = this.CreateBindingSet<MainViewController, MainViewModel>();
set.Bind(Label).To(vm => vm.Title);
set.Apply();
Android:
local:MvxBind="Text Title"
30
Device & platform fragmentation
31
■Customize view presentation
■Platform-specific
■Still retain View Model logic
■Presentation hints
The solution?
MvvmCross presenters
3232
Let’s take a look at
some samples
3333
Hamburger
menu Segmented
control
Tab bar
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>
3535
Navigation
drawer Tabs
Floating
action button
3636
Android fragment presenter
// To use a custom presenter override the IMvxAndroidViewPresenter in your Android setup.cs
protected 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>
37
■ Tabs / Panorama
■ Split View / Master-Detail
■ Fragments
■ Modals
■ Hamburger menu
Technical implementations
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 reuse
Using the presenters saves
you budget because you only
need to code navigation once
Recap of MvvmCross presenters
■ 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> 39
Generics
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)
public class MyViewModel : MvxViewModel
{
public MyViewModel(IMvxJsonConverter jsonConverter,
IMvxGeoLocationWatcher locationWatcher)
{
// Do stuff....
}
}
41
Dependency Injection
■ Accelerometer
■ Download Cache
■ Email
■ File
■ Json
■ Localization
■ Location
Plugins available at
https://github.com/MvvmCross/MvvmCross-Plugins
■ Messenger
■ Phone Call
■ Picture Chooser
■ SQLite
■ Visibility
■ Web Browser
+ Many More!
42
MvvmCross
Plugins
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
■ V7 AppCompat
- MvxCachingFragmentActivity
- android:Theme
- Toolbar
- DrawerToggle
■ Core.UI
- SwipeRefresh
- DrawerLayout
■ Core.Utils
■ RecyclerView
- ItemTouchHelper for Xamarin
- Multiple item templates
■ Design
- NavigationView
- FloatingActionButton
■ Compat
■ Media.Compat
■ LeanBack
44
Material Design support for MvvmCross
See it in action,
demo time!
45
Other Material &
Android support libraries
■ Cardview
■ Pallete
■ Gridlayout
■ Mediarouter
■ V8 Support
■ V13 Support
■ Annotations support
■ Custom tabs
■ Percent support
■ Recommendation support
46
4747
Testing
using MvvmCross.Test.Core;
using Moq;
using NUnit.Framework;
[TestFixture]
public class MyTest : MvxIoCSupportingTest
{
[Test]
public void TestViewModel()
{
base.Setup(); // from MvxIoCSupportingTest
// your test code
}
}
■ Github.com/MvvmCross
■ MvvmCross.com
■ Slack (#mvvmcross)
■ Stackoverflow
■ Xamarin Forums
MvvmCross
Resources
48
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
Get help on Slack
xamarinchat.herokuapp.com
#MvvmCross channel
Follow influencers
#MvvmCross
@Mhvdijk
@Cheesebaron
@MvvmCross
Join the LinkedIn group
linkedin.com/groups/8456977
MvvmCross & Xamarin group
Contribute on Github
github.com/MvvmCross/MvvmCross
50
Get involved!
Questions?
Martijn van Dijk
github.com/martijn00
speakerdeck.com/martijn00
@mhvdijk
mhvdijk@gmail.com
51

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

Editor's Notes

  • #6 UI build natively per platform, leveraging C# C# + XAML C# + XML C# + XIB One shared app logic code base, iOS, Android, Mac, Windows Phone, Windows Store, Windows
  • #9 UI build natively per platform, leveraging C# C# + XAML C# + XML C# + XIB One shared app logic code base, iOS, Android, Mac, Windows Phone, Windows Store, Windows
  • #13 These rules apply to basically all software projects, but we’ve seen that on a mobile project the cost of making the wrong decision are very high
  • #15 Separated Presentation View is platform specific, all other are shared across platforms Other techniques instead of Mvvm File Linking Shared Code Projects #IF #ENDIF Portable Class Libraries
  • #18 Stuart used it as a framework for some Star Wars app he made with some other guys
  • #21 Extended iOS support Xamarin.Apple PCL support Bait and switch for plugins
  • #23 Over 100 contributors over time
  • #24 Over 100 contributors over time
  • #25 Used by many enterprise companies Microsoft Xamarin inc. Dutch government Olo Nokia
  • #26 Over 100 contributors over time Microsoft used it to demo UWP at Build 2015
  • #27 Android Android Support libraries Windows WPF UWP (PC, Mobile, Xbox, TV, IoT) Silverlight Console Xamarin.Forms Uses MvvmCross ViewModels and helpers Uses MvvmCross presenter UI is done in Xamarin.Forms itself tvOS Google wear iWatch
  • #28 Most classes can be inherited, many standard interfaces can be implemented Most methods can be overridden if needed
  • #31 All form factors All OS
  • #34 iOS Hamburger menu Tabbar Segmented control
  • #36 iOS Hamburger menu Tabbar Segmented control
  • #40 Advantage is that when using ViewModel in the View’s code behind all functions are known. You don’t need to cast. Don’t use it too much, otherwise the app size will increase
  • #41 Crash reporting: Xamarin Insights switchen naar Google Analytics Implement per OS platform Last-registered wins Dependency Injection to resolve interfaces can be used in the constructor or as Setter Injection
  • #43 Documentation available in readme of every sub folder of repo Other documentation is available in the Wiki of the main repo
  • #44 The MvvmCross Messenger plugin provides an Event aggregation Messenger which is biased towards using Weak references for event subscription. The basic use of the Messenger is: define one or more Message classes for communication between components. These should inherit from MvxMessage
  • #50 .NET Platform Standard has been introduced recently: https://github.com/dotnet/corefx/blob/master/Documentation/project-docs/standard-platform.md Analysis: Xamarin will introduce Roslyn support next release, which will bring source code fixes to Xamarin / the compiler
  • #51 .NET Platform Standard has been introduced recently: https://github.com/dotnet/corefx/blob/master/Documentation/project-docs/standard-platform.md Analysis: Xamarin will introduce Roslyn support next release, which will bring source code fixes to Xamarin / the compiler