MVVM Light V4.4 with Xamarin Android support
I just published MVVM Light V4.4 to Nuget. For now, this is only the “libraries only” package as I still have some testing to do on the full package as well as on the PCL package.
A “formal” list of changes can be seen here.
Possible breaking change
Let’s start with a possible breaking change. After much feedback “from the field”, I decided to change the names of the MVVM Light DLLs to remove the indication of the framework that they are built for. This might require you to update your XAML files accordingly. I am sorry if that causes you some effort, but I think it makes sense, considering that we are encouraged to share some XAML between the platforms (for example between WinStore and WinPhone), and the naming was breaking this.
So if until now you had (for example) GalaSoft.MvvmLight.WP8.dll, the new assembly is now named GalaSoft.MvvmLight.dll only.
Why was there even “.WP8.DLL” in the first place
At some point early in MVVM Light’s life, I found out that many of the support requests were due to people mistakenly using the WPF DLL in Silverlight. When it was failing to build, they would send me a support request. In order to avoid this, I decided to add “WPF” and “SL” in the DLL name to make things clearer. I then continued with “WP7″, “WP8″, “Win8″, etc. By removing this indication, I hope that I won’t see a rise in this kind of support request again… but I think that by now we are used to having multiple XAML-based platforms.
So what’s new in V4.4
I decided to publish V4.4 even though the “full” package is not ready yet because I had a lot of requests for a Xamarin version of MVVM Light. V4.4 now supports Xamarin Android (see below the paragraph about iOS).
With V4.4, you can do the following steps to add MVVM Light support to a Xamarin application. This small example shows how to create a ViewModel with an observable property and a command, use the VM in an Activity, and add a binding between the observable property and the text of a Button. It also shows how to execute the Command when the button is clicked.
- Create a new Android application.
- Add the package “MvvmLightLibs” to your application.
- Add a new folder named ViewModel.
- Add a new class named MainViewModel in this folder.
- Edit the MainViewModel class to look like below. Note that you can use the “mvvminpcsetlambda” and the “mvvmrelay” code snippets of MVVM Light to add the observable property and the command.
public class MainViewModel : ViewModelBase { private int _index; public const string HelloPropertyName = "Hello"; private string _hello = "Hello!"; public string Hello { get { return _hello; } set { Set(() => Hello, ref _hello, value); } } private RelayCommand _incrementCommand; public RelayCommand IncrementCommand { get { return _incrementCommand ?? (_incrementCommand = new RelayCommand( () => { Hello = string.Format( "Hello! {0} click(s)", ++_index); })); } } }
- Edit the MainActivity as follows:
[Activity( Label = "AndroidApplication4", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity { private MainViewModel _vm; private Button _myButton; public MainViewModel Vm { get { return _vm ?? (_vm = new MainViewModel()); } } public Button MyButton { get { return _myButton ?? (_myButton = FindViewById<Button>(Resource.Id.MyButton)); } } protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); this.AddBinding( () => Vm.Hello, () => MyButton.Text); MyButton.AddCommand( "Click", Vm.IncrementCommand); } }
- Run the application and click the button. This will actuate the RelayCommand, modify the observable property, which will raise the PropertyChanged event. The data binding will react and update the Button.Text property accordingly.
What are these “AddBinding” and “AddCommand” methods?
In short (because I will write a lot more about this), these are extension methods which are specific to MVVM Light for Xamarin.
AddBinding creates a data binding between two properties. There are multiple ways to create data bindings using this method (for example with BindingMode.OneTime, OneWay, TwoWay) and more guidance will follow soon.
AddCommand binds an ICommand to a UI element, and actuates the command when the corresponding event is raised. Note that this can be any event, and that CommandParameters are also supported. Here too, more guidance will come up soon.
What’s next for Xamarin Android?
In the next few days I will publish a much more complete sample showing how to create a full blown MVVM Light application using SimpleIoc, RelayCommand, ViewModelBase and ObservableObject, a simple NavigationService, two-way Binding etc.
The next step for MVVM Light for Xamarin Android will be to publish a project template and a few item templates specifically for MVVM Light Android. This will allow you to do “File, New Project” in Visual Studio and to get a ready-to-work MVVM Light application.
You will also get item templates for a new ViewModel, a new ViewModelLocator, and a new Activity.
What about iOS?
I want to offer the same level of comfort on Xamarin iOS as on Android. This will however take more time. For multiple reasons, it was much easier to start with Android, and I think it makes sense to publish the Android version (and gather feedback) before the iOS version is complete. This is a work in progress.
And Portable Class Libraries?
Here too, this will follow soon. I decided to publish the non-PCL version first as this was a very frequent request, and the PCL version needs a little more testing first. Stay tuned.
Use with caution! Feedback please!
The Android version of MVVM Light is clearly a work in progress, and I would like to advise caution when using it. While the components (SimpleIoc, Messenger, RelayCommand etc) should work just fine, the Binding library is a brand new development. I have ran quite a lot of tests, but I fully expect it to change in the next few weeks/months.
This is where you play a big role, dear Xamarin developers! Please provide feedback! The best place to do that is on Codeplex (http://mvvmlight.codeplex.com) where we can have a discussion. I will follow this attentively.
As usual, thanks for your patience and for your amazing enthusiasm. I cannot wait to see what you guys create with MVVM Light for Xamarin.
Happy coding
Laurent