Don of the Day

Don of the Day

Adventures in software development with Xamarin and the Web

Software developer, building things with code in sunny Scottsdale, AZ.



Using FreshMVVM With Xamarin.Forms

Don FitzsimmonsDon Fitzsimmons

One of the best things about Xamarin.Forms is data binding. Why? Because data binding provides us with a really clean UI paradigm: MVVM (Model View View-Model). You don't even need a framework to use MVVM with Xamarin. You can just have your view models implement INotifyPropertyChanged and away you go.

But, with bare-bones MVVM, there will still be questions remaining. 1) How do you handle navigation within your view models? 2) How do manage your view model dependencies? 3) Where do you initialize things like network calls in your view models (not in your constructors)? 4) And, how do you handle things like alerts?

These are just a few considerations you have to keep in mind when working on a Xamarin.Forms app with a home-grown MVVM implementation and they can all be overcome with some clever code, but why do that when you can use an MVVM framework? And there are several of them out there. I ended up using FreshMVVM for my latest project and I ported an older project to it as well.

So, what's great about this library? First and foremost, it was created specifically for Xamarin.Forms and it shows. I like that. FreshMVVM is very good at solving the common problems that we encounter developing a Xamarin.Forms app. A few of the problems it solves are:

PageModel to PageModel Navigation

In FreshMVVM parlance, a "PageModel" is a view model...conventions matter. FreshMVVM uses a nice abstraction over navigation so you don't have to reference anything related to actual views in your view models...or, PageModels. So you can navigate like this within your view models:

await CoreMethods.PushPageModel<QuotePageModel>();

Automatic wiring of BindingContext

This means that by using the naming convention prescribed by the framework, you don't have to manually bind your view model to the view. It happens behind the scenes, which is nice.

Automatic wiring of Page events

To me, this is a big deal. Not having page events inside of view models is a pain. FreshMVVM adds page lifecycle events to your view models, making things much easier and your code much cleaner:

public virtual void Init(object initData) {}

protected virtual void ViewIsDisappearing (object sender, EventArgs e){}

protected virtual void ViewIsAppearing (object sender, EventArgs e){}

Built-in IOC Container with Constructor Injection

FreshMVVM uses TinyIOC behind the scenes to manage dependencies. It's light-weight and pretty flexible. You then get constructor injection in your view models, just like you'd expect.

And More

These are just a few of the things FreshMVVM handles for you. It does a lot more too. If you're starting up a new Xamarin.Forms app, you should seriously consider using FreshMVVM. Big thanks to Michael Ridland for creating and maintaining this project. Here's a nice video to wet your appetite.

Side Note

I'm well aware that several other - more mature - MVVM frameworks exist like Prism, MVVMLight and MVVMCross. These are all great frameworks and have a lot going for them, but I like the simplicity of FreshMVVM. Hey, it's good to have choices.

Software developer, building things with code in sunny Scottsdale, AZ.