Xamarin & MvvmCross Handbook
  • Introduction
  • Using MvvmCross to create your first Xamarin App
    • Creating the Core project
      • Understanding MVVM
      • Adding a simple Service
      • Adding a ViewModel
      • Adding an App class
    • Creating the Android project
      • Adding an Android Application class
      • Adding the Android Layout View (AXML)
      • Understanding the data-binding syntax
      • Adding the View class
    • Creating the iOS project
      • Updating the AppDelegate class
      • Adding the iOS View for the first ViewModel
      • Understanding the data-binding syntax
    • Creating the UWP project
      • Updating the App.xaml.cs and the App.xaml
      • Adding the UWP View
      • Understanding the data-binding syntax
    • Creating the REST API Service
      • Creating the models
      • Creating the interface and the implementation for the PostService
      • Creating the ViewModel
      • Adding the View for the Android project
      • Adding the View for the iOS project
      • Adding the View for the UWP project
    • Navigation
      • Creating the PostDetail View & ViewModel
      • Command with the parameter
      • Retrieving the param from the previous ViewModel
      • Understanding the IMvxNavigationService
      • Responding the events from different controls in the ListView
      • Retrieving the return result from the previous ViewModel
    • Creating the Xamarin.Forms project
      • Creating the Forms.UI project
      • Adding the App.xaml and the App.xaml.cs
      • Adding the View
      • Creating the Android project
      • Creating the iOS project
      • Creating the UWP project
      • Summary For Forms
    • Summary
  • Implementing MasterDetail layout in Xamarin.Forms by MvvmCross
    • Introduction
    • Creating the project by MvxScaffolding
    • Creating the MasterDetailPage
      • Creating the ViewModel
      • Creating the XAML file
    • Creating the MasterPage
      • Creating the ViewModel
      • Creating the XAML file
    • Creating the DetailPages
      • Creating the ViewModels
      • Creating the XAML files
    • Implementing the Menu functionalities
      • Displaying the MasterPage and the DetailPage
      • Setting the menu navigation
      • The other approaches to set the data-binding
    • Fine-tuning the UI
      • Adding the hamburger icon for iOS
      • Adding the header bar for Android and iOS
      • Adjust the height of the item for UWP
    • Summary
Powered by GitBook
On this page

Was this helpful?

  1. Using MvvmCross to create your first Xamarin App
  2. Creating the REST API Service

Adding the View for the Android project

Add a button to the FirstView.axml in the Resources\layout folder of the MvvmCrossDemo.Droid project, and set the command binding like this:

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        local:MvxBind="Text Greeting" />
    <Button android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Post List"
        local:MvxBind="Click NavToPostListAsyncCommand">
</LinearLayout>

It is time to bind the data to the PostListView. Add a new AXML file called PostListView.axml into the Resources/layout folder in the MvvmCrossDemo.Droid project. Replace the content with the following codes:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <MvvmCross.Platforms.Android.Binding.Views.MvxListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    local:MvxBind="ItemsSource PostList"
    android:layout_weight="1">
  </MvvmCross.Platforms.Android.Binding.Views.MvxListView>
</LinearLayout>

Then Add an AXML file called post_item.axml to show the post item, like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    local:MvxBind="Text Title" />
</LinearLayout>

Now set the item template of the MvxListView, as shown below:

<MvvmCross.Platforms.Android.Binding.Views.MvxListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    local:MvxBind="ItemsSource PostList"
    android:layout_weight="1"
    local:MvxItemTemplate="@layout/post_item">
 </MvvmCross.Platforms.Android.Binding.Views.MvxListView>

You can only name the item template file to lowercase according to the convention of MvvmCross, otherwise MvvxCross cannot find the correct template layout file.

Add a new class called PostListView.cs into the Views folder, which is used to build the connection between the ViewModel and the View. Replace the content with the following codes:

using Android.App;
using Android.OS;
using MvvmCross.Platforms.Android.Presenters.Attributes;

namespace MvvmCrossDemo.Droid.Views
{
    [MvxActivityPresentation]
    [Activity(Label = "MvvmCross Demo", MainLauncher = true)]
    public class PostListView : BaseView
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.PostListView);
        }
    }
}

Do not forget to enable the permission to connect to the Internet. Right-click the MvvmCrossDemo.Droid project and select properties, then select Android Manifest tab and update the Required permissions. Make sure the INTERNET option is checked.

Run the App and click the PostList button to navigate to the PostListView. Now the List should show the correct data from the API, as shown below:

Be careful, we should not call any heavy operations in the first View because it is not good for the user experience.

By the way, I encountered a strange error here. VS cannot deploy the App to my device (it is a HUAWEI Mate 10) with an error:

[INSTALL_FAILED_ALREADY_EXISTS: Attempt to re-install MvvmCrossDemo.Droid without first uninstalling.

I tried to uninstall the App in the Settings - Applications but it still showed the error. At last, I remembered that there is a second system in my phone, which is a special feature of HUAWEI Mate 10. I entered the second system environment and found that if I uninstalled the App in the default system environment, it still existed in the second system environment. So if you have a phone with the special multiple system feature, make sure to completely uninstall the App in all of these environments.

PreviousCreating the ViewModelNextAdding the View for the iOS project

Last updated 6 years ago

Was this helpful?

We use MvxListView which comes from MvvmCross to show the data. Find more built in binding controls here: .

https://www.mvvmcross.com/documentation/fundamentals/data-binding