Creating the Android project

Add a new Xamarin Android project with name MvvmCross.Forms.Droid into the solution. DO NOT select the Android XAML App (Xamarin.Forms) template item.

Select the Blank App template:

Install the Xamarin.Forms package from the NuGet Package Manageer, or input the command below in the NuGet Manager Console:

Then install the MvvmCross.Forms package from the NuGet Package Manager by searching MvvmCross.forms:

If you prefer the Package Manager Console, please use this command:

It will take a while since it need to install some related packages. Next, add the references to both MvvmCrossDemo.Core project and MvvmCrossDemo.Forms.UI project:

Also, we need to add the reference to Mono.Android.Export assembly:

Delete the activity_main.axml file in the Resources\layout folder because we use the MvvmCrossDemo.Forms.UI project to host the UI.

Open the MainActivity.cs file and delete the automatically generated methods and make it to inherit from MvxFormsAppCompatActivity<MvxFormsAndroidSetup<Core.App, UI.App>, Core.App, UI.App> instead of AppCompatActivity, as shown below:

The base class of the MainActivity shows that the Android project uses the default Setup class to initialize itself. Notice that now we have two App classes, one is in the MvvmCrossDemo.Core project, another is in the MvvmCrossDemo.Forms.UI project.

Next, we need to define an AppCompat theme by adding a new XML file called styles.xml into the Resources/values folder. If it already exists, open it and replace the content with the following code:

You can find the code of the style.xml here: https://github.com/MvvmCross/MvvmCross/blob/develop/ContentFiles/Forms/AndroidContent/styles.xml.pp. Because in Android, we use Toolbar instead of the ActionBar so we must set this item:

In this file, you can also customize some values for the style of the Android project. Apply this style file by updating the attributes of the MainActivity class in the MainActivity.cs file, as shown below:

Now you can launch the MvvmCrossDemo.Forms.Droid Project. The result is:

Last updated

Was this helpful?