This post was originally published in 2016 and may contain outdated information.
I’m proud to annonce the release of Pillar, a lightweight MVVM framework for Xamarin.Forms 1.x and 2.x. With this framework, you won’t have to deal with page navigation or messed up code-behind anymore. Now, it’s all about view models, and navigation between view models. It rely on Autofac for dependency injection and MvvmLight for basic MVVM features and helper classes.
Features included in Pillar:
- ViewModel navigation, you won’t need to manipulate pages in your view models
- Design your apps with unit testing in mind with dependency injection
- Flexible, you can use differents patterns: ViewModel first, Messaging, ViewModelLocator
- EventToCommand behavior and useful converters included
- Useful views: ItemsView repeater, with optional DataTemplate selector by item type
- Not intrusive, you can reuse your view models in other projects (WPF for example) with very few modifications
Getting started
You can install Pillar with the Nuget package manager:
Install-Package Askaiser.Mobile.Pillar
You will find the documentation and samples on the GitHub repository.
Quick example
Setup and configuration:
public class MyAppBootstrapper : PillarBootstrapper
{
private readonly Application _app;
public MyAppBootstrapper(Application app)
{
_app = app;
}
protected override void ConfigureContainer(ContainerBuilder builder)
{
base.ConfigureContainer(builder);
builder.RegisterType<FirstViewModel>();
builder.RegisterType<FirstView>();
builder.RegisterType<SecondViewModel>();
builder.RegisterType<SecondView>();
}
protected override void RegisterViews(IViewFactory viewFactory)
{
viewFactory.Register<FirstViewModel, FirstView>();
viewFactory.Register<SecondViewModel, SecondView>();
}
protected override void ConfigureApplication(IContainer container)
{
var viewFactory = container.Resolve<IViewFactory>();
var page = viewFactory.Resolve<FirstViewModel>();
_app.MainPage = new NavigationPage(page);
}
}
public class App : Application
{
public App()
{
new MyAppBootstrapper(this).Run();
}
}
Your first ViewModel:
public class FirstViewModel : PillarViewModelBase
{
private readonly INavigator _navigator;
public FirstViewModel(INavigator navigator)
{
_navigator = navigator;
// Go to the second view after 5 seconds
GoToSecondViewModel();
}
public async void GoToSecondViewModel()
{
await Task.Delay(TimeSpan.FromSeconds(5));
await _navigator.PushAsync<SecondViewModel>();
}
}