1
votes

I try to bind the selected item of a MvxListView to my property. This is my Layout:

<?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:fitsSystemWindows="true"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <include
        layout="@layout/toolbar_actionbar" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="6dp"
        android:paddingRight="6dp">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_search_text"
            local:MvxBind="Text SearchText" />
        <MvxListView
            android:id="@+id/category_list"
            android:orientation="vertical"
            android:choiceMode="singleChoice"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            local:MvxItemTemplate="@layout/listitem_category"
            local:MvxBind="ItemsSource Categories; SelectedItem SelectedCategory" />
    </LinearLayout>
</LinearLayout>

The ItemSource here is properly bound.

ViewModel:

[ImplementPropertyChanged]
public class SelectCategoryListViewModel : AbstractCategoryListViewModel
{
    /// <summary>
    ///     Creates an CategoryListViewModel for the usage of providing a category selection.
    /// </summary>
    /// <param name="categoryRepository">An instance of <see cref="IRepository{T}" /> of type category.</param>
    /// <param name="dialogService">An instance of <see cref="IDialogService" /></param>
    public SelectCategoryListViewModel(IRepository<Category> categoryRepository,
        IDialogService dialogService) : base(categoryRepository, dialogService)
    {}

    public Category SelectedCategory { get; set; }

    public MvxCommand DoneCommand => new MvxCommand(Done);

    public MvxCommand CancelCommand => new MvxCommand(Cancel);

    private void Done()
    {
        MessageHub.Publish(new CategorySelectedMessage(this, SelectedCategory));
        Close(this);
    }

    private void Cancel()
    {
        Close(this);
    }
}

Not the notify PropertyChanged is done via Fody and Categories are in the parent VM.

public class SelectCategoryListActivity : MvxFragmentCompatActivity<SelectCategoryListViewModel>
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.fragment_category_list);

        SetSupportActionBar(FindViewById<Toolbar>(Resource.Id.toolbar));
        SupportActionBar.SetDisplayHomeAsUpEnabled(true);
    }
...

The Warning I get is:

[0:] MvxBind:Warning: 5.11 Failed to create target binding for binding SelectedItem for SelectedCategory

Ideas?

1

1 Answers

3
votes

I just came across a similar issue with a SelectedItem binding failing. The solve was to update Mvx to version 4.1.6. The reason being is that you need to register MvxAppCompatSetupHelper.FillTargetFactories, with 4.1.6 this is now done automatically.

Alternatively, you can manually register it in you setup.cs by overriding FillTargetFactories:

protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
    MvxAppCompatSetupHelper.FillTargetFactories(registry);
    base.FillTargetFactories(registry);
}