Daniel Hindrikes
Developer and architect with focus on mobile- and cloud solutions!
Developer and architect with focus on mobile- and cloud solutions!
Last week I wrote a blog post about Xamarin.iOS and MvvmLight. Now it's time for the second post about MvvmLight, this time about how to use it with Xamarin.Android.
Because I put the ViewModels in a separate project I can use the same ViewModels for both Android and iOS.
First we install the NuGet package for MvvmLight to the Android project.
Install-package MvvmLightLibs
The ViewModels that we will use is the same as in the iOS app and it will look like this.
public class MainViewModel : ViewModelBase
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
RaisePropertyChanged("Name");
}
}
public RelayCommand Send
{
get
{
return new RelayCommand(() =>
{
var nav = ServiceLocator.Current.GetInstance();
nav.NavigateTo(Views.Hello.ToString(), Name);
});
}
}
}
MvvmLight has a INavigationService interface that uses for navigation and each platform will have their own implementation. For Android we will do the configuration in MainActivity. Important is to check if navigation already has been initialized. The code will just run once.
if(!_isInitialized)
{
var nav = new NavigationService();
nav.Configure(Core.Views.Main.ToString(), typeof(MainActivity));
nav.Configure(Core.Views.Hello.ToString(), typeof(HelloActivity));
_isInitialized = true;
}
In my example I using Autofac for IoC, we can also use the IoC container that is in the MvvmLight package. When we have created the NavigationService we had to register it in the IoC container as a INavigationService.
var builder = new ContainerBuilder();
builder.RegisterInstance(nav);
builder.RegisterType();
builder.RegisterType();
var container = builder.Build();
var serviceLocator = new AutofacServiceLocator(container);
ServiceLocator.SetLocatorProvider(() => serviceLocator);
To navigate we will resolve the INavigationService interface and use the NavigateTo method.
var nav = ServiceLocator.Current.GetInstance();
nav.NavigateTo(Views.Hello.ToString(), "Navigation paramter");
To retrieve the parameter we are using the GetAndRemoveParameter in the NavigationService class. Note that this is an Android specific method so we have to cast the INavigationService to NavigationService.
var nav = (NavigationService)ServiceLocator.Current.GetInstance();
var param = nav.GetAndRemoveParameter(Intent);
ViewModel = ServiceLocator.Current.GetInstance();
ViewModel.Name = param;
When using MVVM we want to use data bindings. In Android we have to create the bindnings in code. MvvmLight will help us with that. In the class for the Activity we hade to add a using to the MvvmLight helpers.
using GalaSoft.MvvmLight.Helpers;
The activity also has to inherit from ActivityBase (GalaSoft.MvvmLight.Views.ActivityBase).
public class HelloActivity : ActivityBase
{
The MvvmLight helper namespace will contains the extension methods SetBinding and SetCommand.
The fields that vi are creating bindings to need to be declared as public in the Activity.
public EditText Text { get; private set; }
protected override void OnCreate(Bundle bundle)
{
var button = FindViewById
The SetCommand method's first argument will be which event that will execute the command.
I create the ViewModel in the OnCreate method using the ServiceLocator, I prefer to create it with the ServiceLocator directly instead of wrapping it in a ViewModelLocator which is a common way to do it when using MvvmLight.
The complete code for this sample is on GitHub, https://github.com/dhindrik/XamarinMvvmLightSample