JDibble
JDibble

Reputation: 744

MVVMCross Exception when inflating a Layout with binding

I am developing an android app using Xamarin and MVVMCross.

I am getting the following exception when trying to inflate a layout that is meant to provide the user with a selection list. How can I resolve or work round this issue

Exception:

Cirrious.CrossCore.Exceptions.MvxException: bindingContext is null during MvxAdapter creation - Adapter's should only be created when a specific binding context has been placed on the stack

Source:

var alertBuilder = new AlertDialog.Builder(this);
var view = LayoutInflater.Inflate (Resource.Layout.HeadPartSelectionView, null);

Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res/xxxxxxxxx.xxx.Android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/redpink"
        android:id="@+id/layoutCancelNextButtons">
        <Button
            android:text="Cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/buttonCancelHeadPart"
            android:layout_weight="1"
            android:textColor="@color/white"
            android:background="@color/redpink" />
        <Button
            android:text="Next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/buttonCancelHeadPart"
            android:id="@+id/buttonSelectHeadPart"
            android:layout_weight="1"
            android:textColor="@color/white"
            android:background="@color/redpink" />
    </LinearLayout>
    <Cirrious.MvvmCross.Binding.Droid.Views.MvxListView
        android:layout_width="fill_parent"
        android:layout_height="120dp"
        android:id="@+id/listHeadPart"
        android:layout_below="@id/layoutCancelNextButtons"
        android:textColor="@color/darkgrey"
        android:background="@color/white"
        local:MvxBind="ItemsSource SubAreas"
        local:MvxItemTemplate="@layout/list_headpicklist" />
</RelativeLayout>

ListView layout file

<?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/xxxxxxxxx.xxx.Android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_gravity="center"
        android:textColor="@color/darkgrey"
        local:MvxBind="Text Name"
        android:textSize="24dp" />
</LinearLayout>

Upvotes: 4

Views: 2087

Answers (1)

Kiliman
Kiliman

Reputation: 20312

This is how I create dialog fragments. You need to use BindingInflate() not the standard LayoutInflater.

public class MyDialogFragment : MvxDialogFragment
{
    public override Dialog OnCreateDialog(Bundle savedInstanceState)
    {
        base.EnsureBindingContextSet(savedInstanceState);
        var view = this.BindingInflate(Resource.Layout.Dialog_MyDialog, null);

        var dialog = new AlertDialog.Builder(Activity);
        dialog.SetTitle("Title");
        dialog.SetView(view);
        dialog.SetNegativeButton("Cancel", (s, a) => { });
        dialog.SetPositiveButton("OK", (s, a) => ViewModel.DoCommand());

        return dialog.Create();
    }
}

Upvotes: 5

Related Questions