MvvmCross – Part One

In Xamarin studio I’ll create a new solution called Jokes with a class library (PCL) called Jokes.Core. The “Core” is the convention that MvvmCross uses for the project containing your business logic, can be changed to something else. After the project is created I’ll add the MvvmCross nuget package. That added references to the MvvmCross HotTuna libraries but also created an App.cs file and a ViewModels/FirstViewModel.cs file. App.cs is the entry point of our application, all it does is to initialize the IoC container that comes with MvvmCross and set the start page for the application, in this case FirstViewModel, which I’ll rename to MainPageViewModel.

Next thing I’ll do is to add Models folder with a Person class. The person class is very simple, first and last name properties and a full name that is the first and the last name concatenated.

Then I’ll go to the MainPageViewModel and I’ll add properties for FirstName, LastName and Greeting. The greeting will be composed from the text “Hello” and the full name of the person. The important thing about the ViewModels is that they need to implement the interface INotifyPropertyChanged which is the interface responsible for making the databinding work. MvvmCross gives us a basic implementation of INotifyPropertyChanged with the base class MvxViewModel.

Now I’ll go ahead and a unit test project called Jokes.Core.Tests and I’ll write a test for the MainPageViewModel that when the FirstName is “John” and the LastName is “Smith” the greeting will be “Hello John Smith”. Test passes.

Next step – I’ll add a new Android project called Jokes.Droid and I’ll add MvvmCross nuget to it. I have to add a reference to the the Core project and rename FirstView to MainPage (by convention that needs to match the MainPageViewModel from the Core project so the view model locator can find it)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s