其他分享
首页 > 其他分享> > android – 使用绑定对布局进行膨胀时的MVVMCross异常

android – 使用绑定对布局进行膨胀时的MVVMCross异常

作者:互联网

我正在使用Xamarin和MVVMCross开发一个Android应用程序.

当我试图给用户提供选择列表的布局时,我得到以下异常.我该如何解决或解决这个问题

例外:

Cirrious.CrossCore.Exceptions.MvxException:在创建MvxAdapter期间bindingContext为null – 只应在特定绑定上下文放置在堆栈上时才创建适配器

资源:

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

布局:

<?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布局文件

<?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>

解决方法:

这就是我创建对话框片段的方式.您需要使用BindingInflate()而不是标准的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();
    }
}

标签:android,xamarin,mvvmcross
来源: https://codeday.me/bug/20190830/1770458.html