MvvmUtils provides a way to quickly integrate MVVM and viewmodel navigation.
Mvvmutils is a mvvm framework. It enables developers to create apps using the MVVM pattern on Xamarin.Forms. This allows for better code sharing by allowing you to share behavior and business logic between platforms.
Among the features Mvvmutils provides are:
- ViewModel to Page bindings the developers has to follow the naming convention.
- Every page has to end with Page and its curresponding viewmodel has to end with ViewModel
- Naming convention is case sensitive.
- Eg: Viewmodel for HomePage should be HomeViewModel. The library will pick the curresponding Page on navigation based on the convention.
- ViewModel to ViewModel navigation
Grab the latest MvvmUtils NuGet package and install in your solution.
Install-Package Xamarin.Forms.MvvmUtils
Make sure that both the shared core project and your application projects include the NuGet.
This is what it would typically look like:
using MvvmUtilsExample.Application_Layer;
using System;
using MvvmUtils;
using MvvmUtils.Forms;
namespace MvvmUtilsExample
{
public partial class App : BaseApplication
{
public App()
{
InitializeComponent();
}
/// <summary>
/// On Start
/// </summary>
protected override void OnStart()
{
RegisterAppStart<LoginViewModel>();
}
}
}RegisterAppStart method will start the application.
This is how a typical ViewModel might look like:
public class LoginViewModel : ViewModelBase
{
public LoginViewModel()
{
}
public override void Initialize()
{
base.Initialize();
}
}If any parameters are passed in the navigation, The type has to mention in the viewmodel definition.
Viewmodel with navigation Parameter sample
public class ImageBrowserViewModel : ViewModelBase<int>
{
public ImageBrowserViewModel()
{
}
public override void Initialize(int parameter)
{
}
}Initiate Viewmodel navigation with parameter
Navigation.Push<ImageBrowserViewModel, int>(3);Viewmodel lifecycle events
ViewCreated
Invoked on view created
ViewAppearing
Invoked on view starting to appear
ViewAppeared
Invoked on view appeared
ViewDisappearing
Invoked on view starting to disapear
ViewDisappeared
Invoked on view disappeared
ViewDestroy
Invoked on view destroyed
Please check the example project for more. Also the pages has to be inherited from the base pages in the MvvmUtils library.