MvvmCross.Platform.Exceptions.MvxException binding

2019-09-05 00:35发布

问题:

I tried to convert MVXActivity into MvxFragment , the application crashes when i try to inflate the layout bindingContext is null during MvxAdapter creation - Adapter's should only be created when a specific binding context has been placed on the stack

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
          base.OnCreate(savedInstanceState);
            return inflater.Inflate(Resource.Layout.BrandView1, container, false);

    }

and my layout looks like below

  <?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <SearchView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        local:MvxBind="Query SearchString" />
    <MvxSpinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ProductSpinner"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="40dp"
        local:MvxItemTemplate="@layout/item_product"
        local:MvxDropDownItemTemplate="@layout/item_product"
        local:MvxBind="ItemsSource Products; SelectedItem SelectedProduct" />
    <!--<Mvx.MvxListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        local:MvxBind="ItemsSource FilteredBrands"
        local:MvxItemTemplate="@layout/item_brand" />-->
</LinearLayout>

回答1:

I figured out that i need to use the BindingInflate .

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        this.EnsureBindingContextIsSet(inflater);
        return  this.BindingInflate(Resource.Layout.BrandView1, container, false);

    }