Android 如何通过代码绑定到MvxRecyclerView ItemsSource?

Android 如何通过代码绑定到MvxRecyclerView ItemsSource?,android,xamarin,binding,mvvmcross,Android,Xamarin,Binding,Mvvmcross,我正在片段中使用MvxRecyclerView: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent

我正在片段中使用MvxRecyclerView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
  android:id="@+id/Headline"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerHorizontal="true"
  android:layout_marginTop="50dp"
  android:text="My Headline" />
<MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerView
  android:id="@+id/RecyclerView"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@id/Headline"
  android:layout_marginTop="50dp" />
</RelativeLayout>

这是片段代码:

[MvxFragment(typeof(MainViewModel), Resource.Id.Main_ViewPager)]
[Register("my.app.fragments.MyFragment")]
public class MyFragment : BaseFragment<MyViewModel>
{
    private MvxRecyclerView recyclerView;

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        base.OnCreateView(inflater, container, savedInstanceState);

        var view = this.BindingInflate(Resource.Layout.MyFragment, null);

        this.recyclerView = view.FindViewById<MvxRecyclerView>(Resource.Id.RecyclerView);
        this.recyclerView.ItemTemplateId = Resource.Layout.MyCell;

        var set = this.CreateBindingSet<MyFragment, MyViewModel>();
        set.Bind(this.recyclerView).For(x => x.ItemsSource).To(x => x.Items);
        set.Apply();

        return view;
    }
}
[MvxFragment(typeof(MainViewModel),Resource.Id.Main\u ViewPager)]
[注册(“my.app.fragments.MyFragment”)]
公共类MyFragment:BaseFragment
{
私人MvxRecyclerView recyclerView;
创建视图上的公共覆盖视图(布局、充气机、视图组容器、捆绑包保存状态)
{
base.OnCreateView(充气机、容器、保存状态);
var view=this.BindingInflate(Resource.Layout.MyFragment,null);
this.recyclerView=view.findviewbyd(Resource.Id.recyclerView);
this.recyclerView.ItemTemplateId=Resource.Layout.MyCell;
var set=this.CreateBindingSet();
set.Bind(this.recyclerView).For(x=>x.ItemsSource).To(x=>x.Items);
set.Apply();
返回视图;
}
}
以及ViewModel:

public class MyViewModel : MvxViewModel
{
    ....

    private ObservableCollection<T> items;
    public ObservableCollection<T> Items
    {
        get { return this.items; }
        set
        {
            this.items = value;
            this.RaisePropertyChanged(() => this.Items);
        }
    }

    ....
}
公共类MyViewModel:MvxViewModel
{
....
私人可观测收集项目;
公共可观测收集项目
{
获取{返回this.items;}
设置
{
这个项目=价值;
this.RaisePropertyChanged(()=>this.Items);
}
}
....
}
问题是MvxRecyclerView不显示任何数据。 但是,当我在axml中定义ItemTemplateId和绑定(例如local:MvxItemTemplate=“…”,local:MvxBind=“ItemsSource Items”)时,它确实存在

我的目标不是在axml中定义它。。我想利用在代码中使用这些属性的优势(例如,重命名或重构问题)


是否可以在代码中定义这些绑定?

在xml中设置MvxItemTemplate,并在片段中创建项绑定。

是否可以共享ViewModel?片段中的代码似乎是正确的。也许你没有使用ObservableCollection?我编辑了这个问题,并添加了MyViewModel中有趣的部分。这是一个ObservableCollection-on iOS绑定在这个ViewModel中工作得很好。输出中有没有给出任何警告?没有,任何属于MvxHmhm的东西可能会这样尝试:this.CreateBinding(recyclerview).For(“ItemsSource”).to(vm=>vm.Items).Apply();