A basic starter kit for building MVVM Xamarin Forms applications with Autofac.
I want to thank Jonathan Yates over at Adventures In Xamarin Forms for the base of this starter pack and for his permission to tweak it and publish it here. His blog post really got me started off on the right foot with using Xamarin Forms in a testable fashion. His posts are better written than I could, so I intend to leave his blog to be the "How and Why", here I will simply explain the tweaks I have added to his base.
The starter kit is available from NuGet. That's all I have to say about that. :)
I've been using Jonathan's base in a fairly large app for work over they last year. Having to register your views to the view model in the View Factory is a small annoyance that I have overcome with a small addition to the framework. By placing all your view models in a namespace that ends with 'ViewModels' and ending their class names with 'VM', and your views similarly in a 'Views' namespace and end the class names with 'View', the framework will automagically pair up the view models with the view and will register them with the view factory. To enable this, you simply need to pass the assembly that contains your view models and view to the constructor of your Bootstrapper. Passing the assembly will also register all the views and view models that abide by the naming convention with the container as well. So in the end, your bootstrapper class could look something like this:
public class Bootstrapper : CoreAutofacBootstrapper
{
public Bootstrapper(Xamarin.Forms.Application app, Assembly autoRegisterAssembly) : base(autoRegisterAssembly)
{
App = app;
}
protected override void ConfigureApplication(IContainer container)
{
var viewFactory = container.Resolve<IViewFactory>();
var mainPage = viewFactory.Resolve<MainPageVM>();
var navigationPage = new XamarinFormsAutofacMvvmStarterKit.Controls.NavigationPage(mainPage);
App.MainPage = navigationPage;
}
private readonly Xamarin.Forms.Application App;
}
If you do not want to utilize the automatic view and view model registration, just call the empty constructor on the CoreAutofacBootstrapper
base class. No matter if you use automatic registration or not, you can always override the ConfigureContainer
method in the bootstrapper to register any additional items you want with the container. Likewise, if you have a view/view model pair that do not follow the naming convention, or if you just want to manage the ViewFactory yourself, you can override the RegisterViews
method in the bootstrapper.
The starter kit provides its own NavigationPage
that extends the NavigationPage
provided by Xamarin Forms and adds calling the OnPushed
, OnPopped
, OnAppearing
, and OnDisappearing
at the correct times. Using that as the navigation page for your app, as in the sample above, enables the invoking of navigation notification callbacks in your view model. Just override the method in your view model, that is all there is to that.
public override void OnPopped()
{
// Dow what you need to in here.
}
Using the starter kit's NavigationPage
also enables the automatic disposal of all view models that implement the IDisposable
interface once they are popped off the navigation stack. This works with view model navigation with the Navigator
as well as with the hardware navigation buttons on Android and Windows Phone.
The starter kit also exposes the IDeviceService interface that can be used to decouple the dependency on the Xamarin Device
static class, simply just inject the IDeviceService
in your view model whenever you would have used the Device
class. This greatly simplifies unit testing if you have to use the Device
static class.
- Interface to expose Xamarin.Forms.Device static class for testing purposes.