Xamarin.Forms is designed with MVVM in mind, and you don’t need a framework to develop a Xamarin.Forms application, with the MVVM pattern. As your application becomes, MVVM Frameworks do contain a lot of things to help you, and are certainly worth a look.
What Xamarin.Forms Includes
In relation to MVVM, Xamarin.Forms includes the following two features.
- Binding Engine
- IoC container
Perfect use for small apps, View, ViewModel and not much else. The IoC container is fast but lacks the ability for dependency injection. With this simple setup you can do.
- View = Any UI and UI related logic
- ViewModel = Visual State and Navigation
- Model = Business Logic and Data
The View and ViewModel are easily bound by doing
this.BindingContext = new MyViewModel();
And in your ViewModel, you can easily do
DependencyService.Get<MyModel>();
To gain access to your model. And it works as expected and is rather lean. For very simple, demo or small apps, this will be a viable option.
Xamarin.Forms MVVM Frameworks Include
When your application gets bigger, or has a larger team, it becomes harder to manage, and due to a lack of separation of concerns, can cause headaches maintaining and upgrading it. The following are the advantages of including an MVVM framework.
Navigation
Most MVVM frameworks include very advanced navigation. Instead of doing this:
await _navigationPage.PushAsync(new Page());
You would do something similar to this.
// MVVMCross await _navigationService.Navigate<MyViewModel>(); // Exrin await _navigationService.Navigate("pageKey");
On top of this, they include more advanced navigational features such as SilentPop, NoHistory and Page caching just for starters. Navigation is the most highly rated reason to use an MVVM framework.
Dependency Injection
Some include a Dependency Injection framework, others get you plug it in. If you want to see some performance metrics on DI Frameworks have a look at IoC Container Performance.
Xamarin.Forms only includes resolving the dependency via
DependencyService.Get<Type>();
Other frameworks allow constructor injection which allows you to do this.
public class MyViewModel(INavigationService navigationService)
This is the preferred approach, allowing you to inject dependencies, and have them clearly visible, rather than hidden inside the ViewModel or Model.
Extendable Binding
MVVMCross has some further binding enhancements. Instead of
Text="{Binding SomeValue}"
You could use
Text="{mvx:MvxBind SomeValue, FallbackValue='Nothing'}"
However MVVMCross at this point doesn’t have the Source property. But you can easily interchange them. Xamarin.Forms binding however, is normally suitable for all situations.
Enhanced Commands
Xamarin.Forms includes a default Command. Commands are what are bound to the Command properties of Elements such as Buttons or things that have a user action associated with them, and executing them via your ViewModel. This includes things such as preventing double clicks automatically, and background threading.
Some additional things are automatically setting an IsBusy flag, or implementing INotifyPropertyChanged, which can also be used with other properties to signify any changes.
Summary
I find that Navigation and Dependency Injection enhancements are core to creating a good architecture, and the Xamarin.Forms library does not have these by default. There are many MVVM frameworks you could use, here is a quick list of some notable ones. The categorization is just my personal opinion. You can use any framework for any type of application, these are the categories I think they fit in.
Most Applications
These are some of the most common and are used for a majority of apps.
- MVVMCross
- FreshMVVM
- ReactiveUI – if you are doing Rx
Light Applications
These frameworks try to keep themselves as light as possible.
Large Applications
While these can be used for any application, they particularly excel in larger apps, where forced structure and isolation are more important.
Cross platform mobile developer who loves delving deep into frameworks and solving problems.