Search code examples
androidxamarinmvvmcrossmvxlistview

MvvmCross MvxListView Items Not Rendering on Android


I seem to be having a strange issue when binding an MvxListView to my ViewModel. I have triple-checked the ViewModel and can confirm that it is wired up correctly, as two other primitive types are binding correctly.

However, when I try to bind to an ObservableCollection (or an MvxObservableCollection), I get no items rendered in the MvxListView. I have followed the debug logs, and can see no MvvmCross errors, neither can I see any Android inflation issues.

I have tried various ways of raising propertychanged, as you will see in my code.

This is driving me crazy! Any help would be greatly appreciated. Perhaps I am missing something silly.

ViewModel:

private MvxObservableCollection<ReminderDto> _reminders;

    public MainViewModel(IApiService apiService)
    {
        _apiService = apiService;
    }

    public override void ViewAppearing()
    {
        Reminders = new MvxObservableCollection<ReminderDto>()
        {
            new ReminderDto() { Description = "Test" },
            new ReminderDto() { Description = "Test" },
            new ReminderDto() { Description = "Test" },
            new ReminderDto() { Description = "Test" },
            new ReminderDto() { Description = "Test" },
            new ReminderDto() { Description = "Test" },
            new ReminderDto() { Description = "Test" },
        };

        RaisePropertyChanged(nameof(Reminders));
    }

    public MvxObservableCollection<ReminderDto> Reminders
    {
        get { return _reminders; }
        set { SetProperty(ref _reminders, value); }
    }

MainView.axml

<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">
<MvxListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            local:MvxBind="ItemSource Reminders"
            local:MvxItemTemplate="@layout/reminder_row" />

Item Template (reminder_row.axml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/reminder_danger"
android:weightSum="6">
<TextView
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minWidth="25px"
    android:minHeight="25px"
    android:id="@+id/textView1" />


Solution

  • The property to load a collection is ItemsSource, not ItemSource. Replace this:

    local:MvxBind="ItemSource Reminders"
    

    With this:

    local:MvxBind="ItemsSource Reminders"