Xamarin.android 绑定ViewModel以查看DataContext

Xamarin.android 绑定ViewModel以查看DataContext,xamarin.android,mvvmcross,Xamarin.android,Mvvmcross,我正在Android应用程序上使用MvvmCross。我有一个全局视图模型和两个子视图模型(两者都源自MvxViewModel): 在我的xml中,我想将两个子ViewModels绑定到我的自定义视图的数据上下文,这些视图来自MvxLinearLayout: <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

我正在Android应用程序上使用MvvmCross。我有一个全局视图模型和两个子视图模型(两者都源自
MvxViewModel
):

在我的xml中,我想将两个子ViewModels绑定到我的自定义视图的数据上下文,这些视图来自
MvxLinearLayout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">
    <MyMvxLinearLayout1
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        local:MvxBind="SubViewModel1" />
    <MyMvxLinearLayout2
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:gravity="center"
        local:MvxBind="SubViewModel2" />
</FrameLayout>


但是
MyMvxLinearLayout1
MyMvxLinearLayout2
内部的绑定不起作用。有什么我遗漏的吗?

在做了一些研究之后,我发现了。在自定义视图中,我添加以下字段/属性:

private readonly IMvxAndroidBindingContext _bindingContext;

[MvxSetToNullAfterBinding]
public object DataContext
{
    get { return _bindingContext.DataContext; }
    set
    {
        _bindingContext.DataContext = value;
    }
}
然后在构造函数中:

_bindingContext = new MvxAndroidBindingContext(context, (IMvxLayoutInflaterHolder)context);
var myView = _bindingContext.BindingInflate(Resource.Layout.my_view, null, true); // myView will now have all the proper binding setup, just add it to your layout now
在.xml文件中:

<MyMvxLinearLayout1
android:layout_width="match_parent"
android:layout_height="match_parent"    
local:MvxBind="DataContext SubViewModel1" />


您的自定义
MvxLinearLayout
是否公开要将子ViewModels绑定到的属性?您需要一个属性来在您的控件中保存ViewModel。这实际上不是我想要做的。我试图绑定到视图的数据上下文。在WPF和windowphone中,这是通过绑定到
DataContext
属性来完成的。
<MyMvxLinearLayout1
android:layout_width="match_parent"
android:layout_height="match_parent"    
local:MvxBind="DataContext SubViewModel1" />